How to Extract Dictionary Values as a List?

The dictionary is one of the important data types of Python used to store the data in key-value pairs. Similarly, Python list data type is utilized to store the same type of data. Sometimes for iteration purposes, the dictionary values are extracted as a list.

To extract dictionary values as a list, the “list()” function, “list comprehension”, and “for loop”, etc are used in Python. 

This post will explain all these methods to extract dictionary values as a list using appropriate examples.

Method 1: Using list() Function

The “list()” function is used to create the list object. The “list()” function is utilized along with the “dict.values()” function to extract the dictionary values as a list. Here is an example:

Example 1: Extract Dictionary as a List

The below code is used to extract dictionary values as a list:

Code:

dict_value = {'Name': 'Alex','Age': 45, 'Height': 56,}
print('Given Dictionary: ', dict_value)
print(type(dict_value))
list_value = list(dict_value.values())
print('\nDictionary Values as a List: ', list_value)
print(type(list_value))
  • The dictionary named “dict_value” is initialized and printed on the console.
  • The “dict.values()” function is utilized to get/obtain the values of dictionary keys.
  • The “list()” function takes the dictionary key values as an argument and returns the list of dictionary values.

Output:

The dictionary values have been extracted as a list.

Example 2: Extract Dictionary as a List of Lists

The below code is used to extract the dictionary as a list of lists:

Code:

dict_value = {'Name': ['Alex','Joseph','Lily','Anna'], 'Age': [22, 45, 23, 18]}
print(dict_value)
print(type(dict_value), '\n')
list_value = list(dict_value.values())
print(list_value)
print(type(list_value))
  • The dictionary with multiple values of the key is initialized in the program.
  • The “dict.values()” is used to retrieve all the values of dictionary keys.
  • The “list()” function takes the dictionary key values as an argument and retrieves the list of lists.

Output:

The given dictionary values have been extracted as a list of lists.

Method 2: Using List Comprehension

A new list can be created from an existing one using list comprehension. The list comprehension and the “dict.values()” function is used to extract the dictionary values as a list. 

Here is an example code:

Code:

dict_value = {'Name': 'Joseph', 'Age': 45, 'Height': 6.7}
print(dict_value)
print(type(dict_value), '\n')
list_value = [item for item in dict_value.values()]
print(list_value)
print(type(list_value))
  • The “dict.values()” is used in the list comprehension to return the dictionary values.
  • In the list comprehension, the “for loop” iterates over/through the dictionary values and retrieves the new list counting dictionary values.

Output:

The dictionary values have been extracted as a list.

Method 3: Using for Loop

The simple “for loop” is also used to get the dictionary values as a list:

Code:

dict_value = {'Name': 'Joseph', 'Age': 45, 'Height': 6.7}
print(dict_value)
print(type(dict_value), '\n')
list_value = []
for i in dict_value.values():
    list_value.append(i)
print(list_value)
print(type(list_value))
  • A dictionary named “dict_value” and a list without any element value (empty list) is initialized.
  • The for loop iterates over/through the return output of the function “dict.values()”. The return value of this function contains dictionary values.
  • In the for loop block, the “append()” function is utilized to add the values to an empty list.
  • Lastly, the extracted list is printed as well as its type to ensure that it is a list.

Output:

The “<class ‘list’>” shows that the extracted content has the list type. 

Method 4: Using * Operator

The Python * operator is used to unpacking the given iterable values and expand them as a separate individual element. The below code is used to extract the given dictionary values:

Code:

dict_value = {'Name': 'Joseph', 'Age': 45, 'Height': 6.7}
print(dict_value)
print(type(dict_value), '\n')
list_value = [*dict_value.values()]
print(list_value)
print(type(list_value))
  • The unpacking operator is used along with the “dict.values()” function.
  • The dictionary values return by “dict.values()” function is unpack using the Python “* operator”.

Output:

The dictionary values have been extracted as a list.

Method 5: Using map() Function

The map() function returns a new object known as the map object by applying the specific function to each element value of the specified iterable. The below code uses the “map()” function along with the “dict.values()” and “list()” functions to extract dictionary values as a list:

Code:

dict_value = {'Name': 'Joseph', 'Age': 45, 'Height': 6.7}
print(dict_value)
print(type(dict_value), '\n')
key_value = list(dict_value.keys())
list_value = list(map(dict_value.get, key_value))
print(list_value)
print(type(list_value))
  • The “dict.keys()” function is utilized to get the dictionary keys and these keys are converted into a list using list() function.
  • The “map()” function takes two arguments: “dict.values()” and “key values”.
  • The “map()” function applies the “dict.values()” function to each of the key values of the dictionary and returns the dictionary keys values.
  • These dictionary values are passed into the “list()” function and converted into a list object.

Output:

The dictionary values have been extracted as a list.

Conclusion

To extract dictionary values as a list, the “list()” function, “list comprehension”, “for loop”, “* operator”, and “map()” function is used in Python. The “list()” function is utilized along with the “dict.values()” to extract dictionary values as a list or list of lists. The “list comprehension”, “for loop”, and other mentioned methods are also used to extract dictionary values as a list.

This guide has presented an in-depth explanation of various methods that are used to extract dictionary values as a list.