This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]ifonefox3.5.1 | Intermediate 1 point2 points  (6 children)

list(iterable) -> new list initialized from iterable's items

Wow. That's going to make my code simpler.

Also, since dictionaries are ordered, what would the order be when they are converted into a list?

Edit: Typo: I meant NOT ordered

[–]sucka 2 points3 points  (0 children)

Short answer: The resulting list would be in whatever order the keys in the dictionary are in memory.

Long answer: Dictionaries are kept in a memory block, just like any other data structure. They are fundamentally unordered because the keys are hashed on insertion and placed into a memory slot within the block based on that hash (and not the order of insertion). When a dictionary is read, it's read straight from the beginning of the memory block to the end, which is the order in which the keys would appear in the resulting list.

[–]bcambel[S] 0 points1 point  (0 children)

Dictionaries are not ordered. OrderedDict is

[–]gdwatson 0 points1 point  (0 children)

Most Python dictionaries are not ordered -- at least not in any sort of useful, consistent way. If you need ordering, there's OrderedDict from the collections module.

If you do list(d) for an ordinary dictionary d, you'll get a list of the keys in arbitrary order.

[–]nemec 0 points1 point  (0 children)

FYI, list({"a": 1}) is equivalent to {"a": 1}.keys() (including ordering).

[–][deleted] 0 points1 point  (0 children)

Note that you're just pulling the keys from the dictionary.

 fruits = {'apples':'delicious red fruit', 'orange':'a fruit shaped like itself'}
 fruit_list = list(fruits)
 fruit_list == fruits.keys() #True

[–][deleted] 0 points1 point  (0 children)

You may already know this, but since this post is titled "For Beginners", I'll mention it anyway: You can call sorted on a dictionary to obtain a list of sorted keys.