Convert a Dictionary to a List of Keys in Python

In Python, you can convert a dictionary to a list of its keys using the keys() method. This method returns a view object containing all the keys in the dictionary, which can be easily converted to a list using the list() function. Below are different ways to extract and store dictionary keys as a list.


Examples

1. Dictionary to a List of Keys using keys() and list() Function

The simplest way to convert a dictionary to a list of keys is by using the keys() method along with list().

</>
Copy
# Creating a dictionary
student_scores = {"Alice": 85, "Bob": 90, "Charlie": 78}

# Extracting keys as a list
keys_list = list(student_scores.keys())

# Printing the list of keys
print("List of Keys:", keys_list)

Explanation:

We define a dictionary student_scores where keys represent student names and values represent their scores. The method keys() extracts only the keys, and we use list() to convert it into a list. The result is stored in keys_list and printed.

Output:

List of Keys: ['Alice', 'Bob', 'Charlie']

2. Dictionary to a List of Keys using List Comprehension

List comprehension provides an alternative way to extract dictionary keys as a list.

</>
Copy
# Creating a dictionary
employee_salaries = {"John": 50000, "Emma": 60000, "Mike": 55000}

# Extracting keys using list comprehension
keys_list = [key for key in employee_salaries]

# Printing the list of keys
print("List of Keys:", keys_list)

Explanation:

We define a dictionary employee_salaries, where keys are employee names and values are their salaries. Using list comprehension, we iterate over the dictionary and extract each key into keys_list.

Output:

List of Keys: ['John', 'Emma', 'Mike']

3. Dictionary to a List of Keys using dict.keys() with * Operator

The unpacking operator * can be used to extract dictionary keys into a list efficiently.

</>
Copy
# Creating a dictionary
product_prices = {"Laptop": 800, "Phone": 500, "Tablet": 300}

# Extracting keys using the * operator
keys_list = [*product_prices]

# Printing the list of keys
print("List of Keys:", keys_list)

Explanation:

We define a dictionary product_prices where keys represent product names and values represent their prices. The * operator unpacks the keys directly into keys_list.

Output:

List of Keys: ['Laptop', 'Phone', 'Tablet']

4. Dictionary to a List of Keys using the map() Function

The map() function can be used to extract keys from a dictionary.

</>
Copy
# Creating a dictionary
country_capitals = {"USA": "Washington", "France": "Paris", "India": "New Delhi"}

# Extracting keys using map()
keys_list = list(map(str, country_capitals.keys()))

# Printing the list of keys
print("List of Keys:", keys_list)

Explanation:

We define a dictionary country_capitals where keys are country names and values are their capitals. The map() function applies str() to each key (though unnecessary in this case) and converts the result into a list.

Output:

List of Keys: ['USA', 'France', 'India']

Conclusion

Python provides multiple ways to convert a dictionary into a list of keys:

  1. keys() with list(): The standard and simplest method.
  2. List Comprehension: A readable alternative for extracting keys.
  3. Unpacking (* Operator): A concise way to extract keys.
  4. map() Function: Useful when transformation of keys is needed.

Each approach has its advantages, but the keys() method is the most widely used for its simplicity and efficiency.