1

I use a tool to reformat my code from Python2 to Python3. One of the things that it always does is replacing

my_dict.items()

by

list(my_dict.items())

What is the motivation behind that? I assume that items() do not guaranty the ordering (it is stochastic, since dictionary is not ordered). So, by using list we order the key, value pairs.

Is my assumption correct?

vaultah
  • 44,105
  • 12
  • 114
  • 143
Roman
  • 124,451
  • 167
  • 349
  • 456

1 Answers1

6

The keys(), values(), and items() methods of list in 3.x return views rather than lists, so to preserve their 2.x behavior we need to convert the view into a list.

If this old behavior is not required (e.g. we only ever iterate over the result) then the call to list() can be removed.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358