tags:

views:

91

answers:

5

Hey again all.

My example dictionary is this

data_dictionary = {1:'blue',2:'green',3:'red',4:'orange',5:'purple',6:'mauve'}

The data_dictionary can have more elements depending on the incoming data . The first value is what we call a payload_index. I always get payload_index 1 to 4 .

I need to assemble a list from this. Pretty easy:

for payload_index in data_dictionary :
            assembled_packet.append(data_dictionary[payload_index])

My problem is I need to always skip the 3rd element. I guess I could do an if but that would be inefficient:

for payload_index in data_dictionary :
            if payload_index <> 3:
                assembled_packet.append(data_dictionary[payload_index])

I could do it in two steps and do the first three elements but the problem is I cannot figure out how to get the rest since the number of elements after the 3rd varies. I tried using an impossibly high index (below) but that obviously fails:

#get element 1 and two
for index in range(0,3):
            assembled_packet.append(data_dictionary[index])

#get element 1 and two
for index in range(4,999):
            assembled_packet.append(data_dictionary[index])

Any ideas? Thanks!

+6  A: 

inefficient? That seems like a premature optimisation! Just use normal code:

for payload_index in data_dictionary:
    if payload_index != 3:
        assembled_packet.append(data_dictionary[payload_index])

or even better:

assembled_packet = [data_dictionary[index] for index in data_dictionary if index != 3]

Of course, you could just simply do:

>>> d = {1:'blue',2:'green',3:'red',4:'orange',5:'purple',6:'mauve'}
>>> d.pop(3)
'red'
>>> list(d.values())        # in py3k; in python-2.x d.values() would do
['blue', 'green', 'orange', 'purple', 'mauve']

P.S. <> is long since deprecated.

SilentGhost
+1. Months after the fact, this little "if" statement will probably do more to communicate the intent of the programmer than two strange "for" loops that just happen to omit an element.
Faisal
Love the pop idea. Thanks!
William T Wild
I like `<>` for `not equal`. And please note, it is not **deprecated** - it is [obsolescent](http://docs.python.org/reference/expressions.html)!
Nas Banov
+1  A: 

To make those for loops work you can do this:

# Get the remaining elements
size = len(data_dictionary)
for index in range(4,size):
            assembled_packet.append(data_dictionary[index])
thegrinner
A: 

The best way i can think of is :

output = [elem for elem in data_dictionary.items() if elem[0]!=3]
Piotr Duda
A: 

from you answer is not obvious if you want to skip firts three items or just the 3rd item. if the former is the case, and you are not sure, that your keys are always 1, 2, 3, you can do something like this:

keys = list(sorted(data_dictionary.keys()))[3:]
for payload_index in keys:
    assembled_packet.append(data_dictionary[payload_index])
mykhal
A: 

My problem is I need to always skip the 3rd element. I guess I could do an if but that would be inefficient:

This is even more inefficient:

for payload_index in data_dictionary :
    ...
        assembled_packet.append(data_dictionary[payload_index])

I'd do it this way (Python >= 2.5):

assembled.extend(color for (number, color) in data.iteritems() if number != 3)

For Python 3.x change iteritems to items.

And don't encode type names/descriptions in variable names. It's evil.

pillmuncher
Thanks. That's not a type name , its what the value is called outside of this context.
William T Wild