Skip to content
Home » How to Get Python dictionary keys as a List

How to Get Python dictionary keys as a List

In this tutorial let’s learn about How to Get Python dictionary keys as a List. Converting from one data type to another is a typical operation in Python because the language includes several data types, each with its own purpose.

Convert Python Dictionary Keys to List

In Python, a Dictionary is a collection of key-value pairs. The values within the dictionary can be accessed using the key. Converting a dictionary to a list in Python might signify a variety of things. The different ways to convert Dictionary keys to list in python are:

  1. Dictionary Keys to List using dict.keys()
  2. Dictionary Keys to List using list Comprehension
  3. Dictionary Keys to List using for loop
  4. Dictionary Keys to List using Unpacking Operator *
  5. Dictionary Keys to List using itemgetter
  6. Dictionary Keys to List using list() Method

Convert Python dictionary keys to List using dict.keys()

Python Dictionary keys may be converted to List using the dict.keys() method, which yields a dict keys object. It is possible to iterate through this object, and passing it to the list() constructor which gives a list object with dictionary keys as items.

Code Example :

myDict = {'a': 1, 'b': 2, 'c': 3 , 'd': 4}

# dict.keys()
keys = list(myDict.keys())
print(keys)

Output :

[‘a’, ‘b’, ‘c’, ‘d’]

Get Python dictionary keys as a List using List Comprehension

List Comprehension can also be used to obtain dictionary keys as list items. If you are not comfortable using the built-in methods or functions then you are pick up list comprehension. The below code explains everything.

Code Example :

my_dict = {'a': 1, 'b': 2, 'c': 3 , 'd': 4}

# list Comprehension
keys = [key for key in my_dict]
print(keys)

Output :

[‘a’, ‘b’, ‘c’, ‘d’]

Convert dictionary keys to List using for loop | Python

If you’re not comfortable with List comprehension, we’ll use a For Loop instead. When you iterate over the dictionary, you get the next key at the end of each iteration. Define an empty list say keys to hold the dictionary keys and append the key on each for loop iteration.

Code Example :

my_dict = {'a': 1, 'b': 2, 'c': 3 , 'd': 4}

# for loop
keys = []
for key in my_dict:
    keys.append(key)

print(keys)

Output :

[‘a’, ‘b’, ‘c’, ‘d’]

Convert Python dictionary keys to List using Unpacking Operator *

The unpacking operator * works with any iterable object, and because dictionaries yield their keys when iterated through, you can easily generate a list by using it within a list literal.

Code Example :

my_dict = {'a': 1, 'b': 2, 'c': 3 , 'd': 4}

# Unpacking Operator
keys = [*my_dict]

print(keys)

Output :

[‘a’, ‘b’, ‘c’, ‘d’]

Adding .keys() i.e. [*my_dict.keys()], may assist to clarify your intent, but it will cost you a function look-up and invocation.

Convert dictionary keys to List using itemgetter in Python

itemgetter from the operator module returns a callable object that uses the operand’s getitem() method to retrieve an item from it. This method is then typecast to list and assigned to dict.items().

Code Example :

from operator import itemgetter

my_dict = {'a': 1, 'b': 2, 'c': 3 , 'd': 4}

# itemgetter()
keys = list(map(itemgetter(0), my_dict.items()))

print(keys)

Output :

[‘a’, ‘b’, ‘c’, ‘d’]

Get dictionary keys as List using list() Method in Python

By default, Iterating over a Python dictionary returns the dictionary keys. Thus, using the list() function with the dictionary object returns a list of dictionary keys. As an example,

Code Example :

myDict = {'a': 1, 'b': 2, 'c': 3 , 'd': 4}

# list()
keys = list(myDict)
print(keys)

Output :

[‘a’, ‘b’, ‘c’, ‘d’]

Conclusion

So now you know 6 methods for getting dictionary keys in the form of a listin Python. Python has the advantage of being able to achieve a single goal using a variety of easy ways.

Similar Posts:

How to Convert Strings to bytes in Python
How To Convert Python Unicode Characters To String | Python