Ways to get Python dictionary keys as a list

Python Dictionary

In this post, we are going to learn about different ways to Ways to get Python dictionary keys as a list. We are going to use different approaches that include dict.keys(), appending to list, unpack operator, and many more.

So let us begin with the different methods.

1. dict.keys() method to get dictionary keys as list

The dict. keys() method of dictionary returns an iterable object that contains all keys of the dictionary. We can easily convert it into a list directly by passing the iterable to the list constructor.

#Program get dictionary key as a list
Langlist = {"C#" : 1, "Rust" : 2, "C++" : 3, "Rust" :2,"Go" :5}
dict_keys_to_list = list(dict.keys(Langlist))
print('dict keys to list',dict_keys_to_list)

Output

dict keys to list : ['C#', 'Rust', 'C++', 'Go']

2. Loop to get Dictionary keys as a list

The basic method in python or any programming language to get keys of the dictionary is simply we can loop over the keys of the dictionary and append them in the list to get dictionary keys as a list. Here we are using for loop to loop over all the Langlist keys and appending each key to the dict_keys_to_list list.

Let us dive into an example to understand it firmly.

#Program get dictionary key as a list
Langlist = {"C#" : 1, "Rust" : 2, "C++" : 3, "Rust" :2,"Go" :5}
#a empty list
dict_keys_to_list = []
for key in Langlist:
 dict_keys_to_list.append(key)
print('dict keys to list :',dict_keys_to_list)

Output

dict keys to list : ['C#', 'Rust', 'C++', 'Go']

3.List Comprehension to get Dictionary keys as List.

The next method to do this is by using List comprehension. List comprehension provides a simple and shorter syntax code to loop over keys and gets the dictionary keys as a list.

Let us understand this with below example code:

#Program get dictionary key as a list
Langlist = {"C#" : 1, "Rust" : 2, "C++" : 3, "Rust" :2,"Go" :5}
#loop over dictionary keys to get as list
dict_keys_to_list = [key for key in Langlist]
print('dict keys to list:',dict_keys_to_list)

Output

dict keys to list: ['C#', 'Rust', 'C++', 'Go']

4. Convert the dictionary to list to get Dictionary keys as List.

The most simple and one-line code that converts a dictionary into a list and gets the dictionary keys as a list is given in the below example. In this code example, we are using the list() constructor and passing a dictionary as an argument.

Note:By default dictionary iteration on dictionary returns its key.

#Program get dictionary key as a list
Langlist = {"C#" : 1, "Python" : 2, "C++" : 3, "Rust" :2,"Go" :5}
#converting dictionary keys to list
dict_keys_to_list = list(Langlist)
print('dict keys to list:',dict_keys_to_list)

Output

dict keys to list: ['C#', 'Python', 'C++', 'Rust', 'Go']

5. Convert Dict. keys() to list to get Dictionary keys as List.

In this code example we are converting dict.keys(Langlist) into the list.It is so simple with just one-line of code we have get the dictionary keys as list.

#Program get dictionary key as a list
Langlist = {"C#" : 1, "Python" : 2, "C++" : 3, "Rust" :2,"Go" :5}
#converting dictionary keys to list
dict_keys_to_list = list(dict.keys(Langlist))
print('dict keys to list:',dict_keys_to_list)

Output

dict keys to list: ['C#', 'Python', 'C++', 'Rust', 'Go']

6. Unpacking(*) operator to get dictionary key as a list

The unpacking(*)operator in python is used to unpack values from the iterable, The single asterisk(*) can be used for any iterable that exist in python. We use a double-asterisk(**) for the dictionary.

In this code example, we are passing, getting all keys of a dictionary bypassing the Langlist, and unpacking its keys by using the asterisk (*) operator into a list.

#Program to get dictionary key as a list

Langlist = {"C#" : 1, "Python" : 2, "C++" : 3, "Rust" :2,"Go" :5}
#converting dictionary keys to list
dict_keys_to_list = [*Langlist]
print('dict keys to list:',dict_keys_to_list)

Output

dict keys to list: ['C#', 'Python', 'C++', 'Rust', 'Go']

Conclusion

We have learned the different Ways to get Python dictionary keys as a list, we hope you find this useful and will use them in your problems.