Chris Dutrow
Chris Dutrow

Reputation: 50362

Iterate over a dict or list in Python

Just wrote some nasty code that iterates over a dict or a list in Python. I have a feeling this was not the best way to go about it.

The problem is that in order to iterate over a dict, this is the convention:

for key in dict_object:
    dict_object[key] = 1

But modifying the object properties by key does not work if the same thing is done on a list:

# Throws an error because the value of key is the property value, not 
#     the list index:

for key in list_object:
    list_object[key] = 1 

The way I solved this problem was to write this nasty code:

if isinstance(obj, dict):
    for key in obj:
        do_loop_contents(obj, key)
elif isinstance(obj, list):
    for i in xrange(0, len(obj)):
        do_loop_contents(obj, i)

def do_loop_contents(obj, key):
    obj[key] = 1

Is there a better way to do this?

Thanks!

Upvotes: 16

Views: 18926

Answers (6)

Barun
Barun

Reputation: 1622

I stumbled upon this post while searching for a better one, here's how I did it.

for row in [dict_or_list] if not type(dict_or_list) is list else dict_or_list:
    for i,v in row.items():
        print(i,v)

Upvotes: 0

ThisIsAQuestion
ThisIsAQuestion

Reputation: 1967

You probably just want to have a different code depending on if the object you are trying to change is a dict or a list.

if type(object)==type([]):
    for key in range(len(object)):
        object[key]=1
elif type(object)==type({}): #use 'else' if you know that object will be a dict if not a list
    for key in object:
        object[key]=1

Upvotes: 1

DSM
DSM

Reputation: 353019

I've never needed to do this, ever. But if I did, I'd probably do something like this:

seq_iter = x if isinstance(x, dict) else xrange(len(x))

For example, in function form:

>>> def seq_iter(obj):
...     return obj if isinstance(obj, dict) else xrange(len(obj))
... 
>>> x = [1,2,3]
>>> for i in seq_iter(x):
...     x[i] = 99
... 
>>> x
[99, 99, 99]
>>> 
>>> x = {1: 2, 2:3, 3:4}
>>> for i in seq_iter(x):
...     x[i] = 99
... 
>>> x
{1: 99, 2: 99, 3: 99}

Upvotes: 15

Markus Unterwaditzer
Markus Unterwaditzer

Reputation: 8244

test_list = [2, 3, 4]
for i, entry in enumerate(test_list):
    test_list[i] = entry * 2
print(test_list)  # Gives: [4, 6, 8]

But you probably want a list comprehension:

test_list = [2, 3, 4]
test_list = [entry * 2 for entry in test_list]
print(test_list)  # Gives: [4, 6, 8]

Upvotes: 1

Silas Ray
Silas Ray

Reputation: 26150

To be pythonic and ducktype-y, and also to follow "ask for forgiveness not permission", you could do something like:

try:
    iterator = obj.iteritems()
except AttributeError:
    iterator = enumerate(obj)
for reference, value in iterator:
    do_loop_contents(obj, reference)

Though if all you need is the key/index:

try:
    references = obj.keys()
except AttributeError:
    references = range(len(obj))
for reference in references:
    do_loop_contents(obj, reference)

Or as a function:

def reference_and_value_iterator(iterable):
    try:
        return iterable.iteritems()
    except AttributeError:
        return enumerate(iterable)

for reference, value in reference_and_value_iterator(obj):
    do_loop_contents(obj, reference)

Or for just the references:

def references(iterable):
    try:
        return iterable.keys()
    except AttributeError:
        return range(len(iterable))

for reference in references(obj):
    do_loop_contents(obj, reference)

Upvotes: 5

Tadeck
Tadeck

Reputation: 137320

This is the correct way, but if for some reason you need to treat these two objects the same way, you can create an iterable that will return indexes / keys no matter what:

def common_iterable(obj):
    if isinstance(obj, dict):
        return obj
    else:
        return (index for index, value in enumerate(obj))

Which will behave in the way you wanted:

>>> d = {'a': 10, 'b': 20}
>>> l = [1,2,3,4]
>>> for index in common_iterable(d):
    d[index] = 0

>>> d
{'a': 0, 'b': 0}
>>> for index in common_iterable(l):
    l[index] = 0

>>> l
[0, 0, 0, 0]

Or probably more efficiently, using a generator:

def common_iterable(obj):
    if isinstance(obj, dict):
        for key in obj:
            yield key
    else:
        for index, value in enumerate(obj):
            yield index

Upvotes: 7

Related Questions