Getting the keys and values of a dictionary in the original order as a list

Hello and a nice week to everyone,

I am trying to extract the keys and values of a dictionary (which has 20 keys and their values), into two separate lists in the order that they appear in the dictionary but it seems like dict.keys() method works differently. How can I achieve that?

Here is my code and what I get:

What I want to get is:

[‘1’, ‘2’, ‘3’]

Hello,

You can use from collections import OrderedDict or wait for Python 3.7 to come to Rhino (maybe never…) https://docs.python.org/2/library/collections.html#collections.OrderedDict

And please don’t use list and dict as variable names because you are overwriting the builtin names :wink:

from collections import OrderedDict

my_dict = OrderedDict([("1","a"),("2","b"),("3","c")])
my_dict["4"] = "d"

print my_dict
print my_dict.keys()

Yields:

OrderedDict([(‘1’, ‘a’), (‘2’, ‘b’), (‘3’, ‘c’), (‘4’, ‘d’)])
[‘1’, ‘2’, ‘3’, ‘4’]

Have a great week!

3 Likes

Well, normally dictionaries are considered “unordered collections”. So maybe you want to use something ordered like a list instead.

It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary).

https://docs.python.org/2/tutorial/datastructures.html#dictionaries

2 Likes

Thanks for the answers and the warning. I’m not using those names in my code, it was used just in this demonstration. :slight_smile:

I understand it and it worked in Python 3.7. The reason I wanted to use a dictionary was because it would be easy for me to make new entries in the future. In case of 2 lists, I have to enter the items in the correct location and make it in two separate lists. But in case of a dictionary, I have to make only one entry, that’s why it seemed easier.

Well, I assumed that was the case if you want the things kept in order. If not a normal dictionary works fine. You can of course sort the keys and then use that sorted list to get the values in sorted order…

Not necessarily. You could also use a nested list.

dict_list=[("1","A"),("2","B"),("3","C"),("4","D"),("5","E")]
print [item[0] for item in dict_list]
>>>['1', '2', '3', '4', '5']
print [item[1] for item in dict_list]
>>>['A', 'B', 'C', 'D', 'E']

You can also use zip() and the * operator to zip and unzip nested lists:

n_list=['1', '2', '3', '4', '5']
a_list=['A', 'B', 'C', 'D', 'E']

zip_list=zip(n_list,a_list)
print zip_list
>>>[('1', 'A'), ('2', 'B'), ('3', 'C'), ('4', 'D'), ('5', 'E')]

nn_list,aa_list=zip(*zip_list)
print nn_list
print aa_list
>>>('1', '2', '3', '4', '5')
>>>('A', 'B', 'C', 'D', 'E')
#creates tuples though, so you need to convert to list if necessary
1 Like

An ordered dict is a dictionary so you can add items later my_dict["4"] = "d"

Where does that put the item in the ‘order’ ?

Puts them next - it continues to respect the order in which they were added

As in the bottom of the stack…

1 Like

yup - clarified in my revised example above

1 Like

Thanks a lot. This solved the problem. And also the code is working faster now. With my original code it was taking about two seconds for my list to appear, now almost instantaneous.