Python | Convert Dictionary Keys to List

List and dictionary store data collection in Python. The list stores data collection in a single variable, while the dictionary stores data in a “key-value” pair format. Every element of the dictionary has a unique key name with its value, and that value will be accessed anywhere in the program using the key name.

This article will explain a complete in-depth overview of how to convert dictionary keys to lists. The following aspects will be demonstrated with examples in this article:

So let’s get started!

Method 1: Using “dict.keys()” Function

The inbuilt “dict.keys()” function is used to get the dictionary keys as an object, and after that, the keys object is passed as an argument value of the “list()” function. In the code given below, the “dict.keys()” and “list()” functions are used to convert dictionary keys to lists.

Code:

Dict_1 = {'Name': 'istLinuxFoss', 'Guide': 'Python'}
list_key = list(Dict_1.keys())
print('Keys Value in List: ',list_key)
print('\n',type(list_key))

In the above code, the dictionary variable named “Dict_1” is defined in the program. The “list()” function is used with another function named “keys()” to convert the keys of the dictionary into the list. The list of Keys is stored in a variable named “list_key” and shown on the console screen.

Output:

The above output shows the list of keys.

Method 2: Using List Comprehension

To create a list from its element or items, list comprehension is used in Python. The list comprehension technique is used in this method to create a list of keys of the given dictionary. Let understand via the following code:

Code:

Dict_1 = {'Name': 'istLinuxFoss', 'Guide': 'Python'}
list_key = [key for key in Dict_1]
print(list_key)
print('\n',type(list_key))

In the mentioned code, the list comprehension is used to create a list of keys from the given dictionary keys. The list comprehension method is a unique way to create a list from its elements or items. The final list of keys is printed on the console output using the print() function.

Output:

A list of keys is displayed in the output.

Method 3: Using for Loop and append() Function

In the example given below, keys are appended to an empty list to create a list of keys. The “for” loop iterates on the given dictionary to return keys, and then all the keys are appended to an empty list. Let’s see an example given below to clarify the working of this method:

Code:

Dict_1 = {'Name': 'Alex', 'Age': 18}

list_key = []
for key in Dict_1:
    list_key.append(key)

print(list_key)
print('\n',type(list_key))

In the above code, the dictionary value and an empty list is initialized. The “for” loop iterates on the dictionary and gets all the keys of the dictionary. The dictionary keys are added to an empty list to create a list of keys.

Output:

A list of keys is displayed in the output.

Method 4: Using Unpacking with *

Unpacking allows us to assign dictionary keys to a list. The unpacking operator * packs multiple key values of the dictionary into a list of keys. Lets understand it via the following example:

Code:

Dict_1 = {'Name': 'Alex', 'Age': 18}
list_key = [*Dict_1]
print(list_key)
print('\n',type(list_key))

In the following code, the dictionary value is initialized and the dictionary is unpacked with *. The dictionary returns its key when iterated, so unpacking with * easily creates a list of keys.

Output:

The output shows the list of keys.

Method 5: Using itemgetter() and dict.items()

In the example given below, the function “itemgetter()” is used to get the dictionary’s key value and mapped into “dict.items()”.

Code:

from operator import itemgetter

Dict_1 = {'Name': 'Alex', 'Age': 18, 'Weight': 6.6}
list_key = list(map(itemgetter(0), dict.items(Dict_1)))
print(list_key)
print('\n',type(list_key))

In the above code, the “itemgetter()” function of the operator module gets the keys of the dictionary. The “map()” function performs operations on each item, and all the keys are converted to a list using the “list()” function.

Output:

A list of keys is displayed in the output.

That’s all from this Python guide!

Conclusion

In Python, the “dict.keys()”, “List Comprehension”, “for loop with append()” function, “Unpacking with *”, and “itemgetter()“ functions are used to convert dictionary keys to lists. The “dict.keys()” gets the keys from the dictionary and converts them to a list using the “list()” function. The “list comprehension” creates a list of keys from the given dictionary keys using a single-line expression. The “itemgetter()” function gets the keys from the dictionary and maps them into the “dict.keys()” function, the key items are typecast into a list by utilizing the “list()” function. This guide explains all methods in detail with examples to convert dictionary keys to lists.