leafleafleafDocy banner shape 01Docy banner shape 02Man illustrationFlower illustration

Converting Python Dictionaries (dict) to Lists

Estimated reading: 3 minutes 60 views
Converting Python Dictionaries (dict) to Lists

Through some methods, we can achieve mutual conversion between dictionaries and lists.

Python convert dictionary to list

There are 3 dictionary methods: keys(), values() and items(). They can return list-like values, corresponding to dictionary keys, values, and key-value pairs, respectively. But the values ​​returned by these methods are not really lists, because they cannot be modified and there is no append() method.

However, we can easily convert their return values ​​into real lists using the conversion function list(). Let’s take the dictionary person as an example.

>>> person={"name":"Johnson","age":9,"gender":"male","height":"140cm"}
>>> person.keys()
dict_keys(['name' , 'age', 'gender', 'height'])
['name', 'age', 'gender', 'height']
>> >list2=list(person.values())
['Johnson', 9, 'male', '140cm']
[(' name', 'Johnson'), ('age', 9), ('gender', 'male'), ('height', '140cm')]

As you can see, the elements in list1 are the keys in the dictionary person; the elements in list2 are the values ​​in the dictionary person; and list3 is a list of nested tuples, and the elements in the list are the keys-values ​​in the dictionary person right.

Dictionaries cannot be sorted, but when there is a sorting requirement, we can convert the dictionary into a list, convert each key-value pair in the dictionary into a two-tuple embedded in the list, and then perform operations such as sorting .

Now, suppose we have a dictionary scoreDict that records scores, the key is the name and the value is the score, and now we want to sort it using the sort() method.

>>> scoreDict={"John":82,"Christina":96,"Johnson":100,"Marry":73,"Emily":88,"Justin":92}
>>> scoreList=list(scoreDict .items())
>>> scoreList.sort()
>>> print (scoreList)
[('Christina', 96), ('Emily', 88), ('John', 82), ('Johnson', 100), ('Justin', 92), ('Marry', 73)]

We can see that now the order of scoreList has changed, it is arranged in ascending order of keys, that is, in alphabetical order of names, namely “Christina” “Emily” “John” “Johnson” “Justin” and “Marry”.

However, sometimes, we need to sort not only by key, but also by value. For example, it may be desirable to rank scores from low to high. When we introduced the sort() method, we mentioned that the sort() method can accept a parameter whose key is the key, and we can specify the element to be sorted by specifying the key as a special lambda expression.

In this example, we specify to use the 2nd element in items, that is, to sort by score, as shown in the code below.

>>> scoreList.sort(key=lambda items: items[1])
>>> print(scoreList)
[('Marry', 73), ('John', 82), ('Emily', 88), ( 'Justin', 92), ('Christina', 96), ('Johnson', 100)]

Tip: lambda expressions are used to create anonymous functions that can take any number of arguments and return the value of a single expression. This concept is a little complicated, we just need to understand it briefly.

Python convert list to dictionary

We can also convert nested lists to dictionaries using the function dict().

>>>list4= [["key1","value1"],["key2","value2"],["key3","value3"]]
>>> dict1=dict(list4)
>>> dict1
{ 'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}

Leave a Comment