William T Wild
William T Wild

Reputation: 1032

Accessing only part of a dictionary in a for using Python

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!

Upvotes: 2

Views: 2314

Answers (5)

pillmuncher
pillmuncher

Reputation: 10162

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.

Upvotes: 0

mykhal
mykhal

Reputation: 19924

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])

Upvotes: 0

SilentGhost
SilentGhost

Reputation: 319879

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.

Upvotes: 6

Piotr Duda
Piotr Duda

Reputation: 1815

The best way i can think of is :

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

Upvotes: 0

thegrinner
thegrinner

Reputation: 12241

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])

Upvotes: 1

Related Questions