How to convert Python Dictionary to a list?



In this article, we will show you how to convert a python dictionary to a list. Below are the methods to accomplish this task:

  • Using list & items() Method

  • Using keys() Method

  • Using values() Method

  • Using List Comprehension

  • Using Zip() Function

  • Using map() Function

  • Using for loop & items() Method

Dictionaries are Python's version of an associative array data structure. A dictionary is a collection of key-value pairs. Each key pair is represented by a key pair and its associated value.

A dictionary is defined by a list of key-value pairs enclosed in curly braces and separated by commas. Each key's value is separated by a colon(:).

A dictionary cannot be sorted solely for the purpose of obtaining a representation of the sorted dictionary. Dictionaries are orderless by definition, while other kinds, such as lists and tuples, are not. As a result, you require an ordered data type, which is a list—most likely a list of tuples.

Python's dictionary class has three methods for this purpose. The methods items(), keys(), and values() return view objects comprising of tuple of key-value pairs, keys only and values only respectively. The in-built list method converts these view objects in list objects.

Using the list & items() Method

Python's dictionary class offers the items() function, which returns an iterable sequence of all key-value pairs in the dictionary (dict items). This retuned sequence represents the actual key-value pairs in the dictionary. We can use the list() function to get a list of tuples from this iterable sequence.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store an input dictionary.

  • Get all the key-value pairs of a dictionary using the items()> function(returns a group of the key-value pairs in the dictionary) and convert the dictionary items(key-value pair) to a list of tuples using the list() function(returns a list of an iteratable).

  • Print the resultant list of a dictionary after converting.

Example

The following program converts python dictionary to a list using list & items() Method −

# input dictionary inputDictionary = {'Hello': 10, 'Tutorialspoint': 20, 'python': 30} # converting input dictionary items(key-value pair) # to a list of tuples resultList = list(inputDictionary.items()) # printing the resultant list of a dictionary print(resultList)

Output

On executing, the above program will generate the following output −

[('Hello', 10), ('Tutorialspoint', 20), ('python', 30)]

Using keys() Method

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input dictionary.

  • Print the list of all the keys of a dictionary with the keys() function(the dict. keys() method provides a view object that displays a list of all the keys in the dictionary in order of insertion) by applying it to the input dictionary and convert the result to a list using the list() function(converts the sequence/iterable to a list).

Example

The following program returns the list of all the keys of a dictionary using the list() and keys() functions −

# input dictionary inputDictionary = {'Hello': 10, 'Tutorialspoint': 20, 'python': 30} # converting input dictionary keys to a list resultList = list(inputDictionary.keys()) # printing the resultant list of a dictionary keys print(resultList)

Output

On executing, the above program will generate the following output −

['Hello', 'Tutorialspoint', 'python']

Using values() Method

To obtain a list of values, use the values() method of dictionaries. We can convert values into a list by using the list() function.

Example

The following program returns the list of all the values of a dictionary using the list() and values() functions −

# input dictionary inputDictionary = {'Hello': 10, 'Tutorialspoint': 20, 'python': 30} # converting input dictionary values to a list resultList = list(inputDictionary.values()) # printing the resultant list of a dictionary values print(resultList)

Output

On executing, the above program will generate the following output −

[10, 20, 30]

Using List comprehension

List comprehension makes it simple to create a list in Python. Furthermore, you can change the value of elements on the same line.

Using dictionary.items() to retrieve the keys and values of the items before adding them to the list.

Example

The following program converts python dictionary to a list using list comprehension −

# input dictionary inputDictionary = {'Hello': 10, 'Tutorialspoint': 20, 'python': 30} # Storing key and value as list of tuples using list comprehension resultList = [(key, value) for key, value in inputDictionary.items()] # printing the resultant list of a dictionary key-values print(resultList)

Output

On executing, the above program will generate the following output −

[('Hello', 10), ('Tutorialspoint', 20), ('python', 30)]

Using Zip() Function

The zip() function can be used to combine two lists/iterators. In this manner, we can combine the dictionary's .keys() and.values() methods to access both values at the same time.

Example

The following program converts python dictionary to a list using list zip() function −

# input dictionary inputDictionary = {'Hello': 10, 'Tutorialspoint': 20, 'python': 30} #combining keys and values of dictionary using zip() function resultList = zip(inputDictionary.keys(), inputDictionary.values()) # converting zip object to list resultList = list(resultList) # printing the resultant list of a dictionary key-values print(resultList)

Output

On executing, the above program will generate the following output −

[('Hello', 10), ('Tutorialspoint', 20), ('python', 30)]

Using map() Function

map() is a built-in function that allows you to map any function over an iterator/list. To create lists of lists, map the list() function over all the elements of dictionary.items().

Example

The following program converts python dictionary to a list using the list map() function −

# input dictionary inputDictionary = {'Hello': 10, 'Tutorialspoint': 20, 'python': 30} # conveting list of keys and values of dictionary to a list using map() function resultList = new_list = list(map(list, inputDictionary.items())) # printing the resultant list of a dictionary key-values print(resultList)

Output

On executing, the above program will generate the following output −

[['Hello', 10], ['Tutorialspoint', 20], ['python', 30]]

Using for loop & items() Method

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input dictionary.

  • Creating an empty list that gives the resultant list of a dictionary key, values.

  • Use the for loop to traverse through each key value pair of a dictionary using items() function(returns a group of the key-value pairs in the dictionary).

  • Use the append() function( adds the element to the list at the end) to append list of corresponding key-value pair to a list.

  • Print the resultant list of a dictionary key-values

Example

The following program converts python dictionary to a list using for loop, append(), items() functions −

# input dictionary inputDictionary = {'Hello': 10, 'Tutorialspoint': 20, 'python': 30} # creating an empty list resultList = [] # traversing through each key value pair of a dictionary using items() function for key, val in inputDictionary.items(): # appending list of corresponding key-value pair to a list resultList.append([key, val]) # printing the resultant list of a dictionary key-values print(resultList)

Output

On executing, the above program will generate the following output −

[['Hello', 10], ['Tutorialspoint', 20], ['python', 30]]

Conclusion

In this article, we learned how to use the seven different methods to convert a given dictionary to a list. We also learned how to use the items() function to iterate through the dictionary.


Advertisements