tudou
tudou

Reputation: 515

Difference between iterate dictionary.items() vs list(dictionary.items())

What is the difference between (the goal is to extract the k,v value)

for k,v in example_dict.items():
    print(k,v)
    do_something

vs

for k,v in list(example_dict.items()):
    print(k,v)
    do_something

The results look same

Upvotes: 1

Views: 2330

Answers (4)

shadowtalker
shadowtalker

Reputation: 13863

The difference is that the list is unnecessary in most cases, and might even be harmful.

The .items() method returns a "view" into the data. As per the documentation:

The objects returned by dict.keys(), dict.values() and dict.items() are view objects. They provide a dynamic view on the dictionary’s entries, which means that when the dictionary changes, the view reflects these changes.

This is not the case when you wrap it in list, which preserves the keys/values/items as they appeared when the list was created.

In general, you should not use the version with list() if you are iterating with for. It's superfluous at best and adds unnecessary visual clutter to the code.

Edit: As per the other answer by Jan Stránský, list() also performs a full iteration pass on the data in order to construct the list. This is quite wasteful!

Upvotes: 6

solid.py
solid.py

Reputation: 2812

By definition, an iterable is any Python object capable of returning its members one at a time, permitting it to be iterated over in a for-loop.

A list is an iterable, however the method dict.items() already returns returns a list or a view of the dictionary's entries depending on the version of the language used.

Python2
If you call list() on a list, such as the one produced from dict.items() then a shallow copy is returned, meaning you will make a new list by the elements of the innermost list. As mentioned in What does the list() function do in Python?

Python3
As mentioned in https://docs.python.org/3.3/library/stdtypes.html#dict-views The objects returned by dict.items() are view objects, and when they are used as an iterable they work the same, e.g when calling list().

In either case, the list() call is redundant at best, and should be avoided.

Upvotes: 1

IPvStack
IPvStack

Reputation: 21

The difference here is the data type. You will not see it when iterating, but you will see it when printing normally:

d = {1: "first", 2: "second", 3: "third"}
d.items()
dict_items([(1, 'first'), (2, 'second'), (3, 'third')])
list(d.items())
[(1, 'first'), (2, 'second'), (3, 'third')]

at the last one I put the dict object (with the items() method) into a list and output it

Upvotes: 2

Jan Stránský
Jan Stránský

Reputation: 1691

The difference is, well, obviously, the use of list.

The first approach is preferable, because example_dict.items() returns a dict view and for k,v in example_dict.items(): iterates "directly" over the items.

With for k,v in list(example_dict.items()): you actually do 2 iterations. First the program do one loop to convert items to list. Then you do a second loop to iterate the list. So it is unnecessary.

Upvotes: 2

Related Questions