In this Python tutorial, we will discuss how to create a list of dictionary keys in python. We’ll talk about the procedures and techniques needed to create a list in Python using dictionary keys.
Recently, I was working on a Python Project where we got one requirement that we had to store unique keys element from the dictionary in a Python list.
Here we will discuss
- How to create a list of dictionary keys in Python using dict.keys()
- Create a list of dictionary keys in Python using * operator
- How to create a list of dictionary keys in Python using list comprehension
- Create a list of dictionary keys in Python using for loop and append()
- How to create a list of dictionary keys in Python using list()
Create a list of dictionary keys in python
In Python, there are primarily three methods that are commonly used and important to understand when creating a list of dictionary keys in Python.
How to create a list of dictionary keys in Python using dict.keys()
- In this section, we will discuss how to create a list of dictionary keys in Python using dict.keys().
- To extract all the keys from the dictionary, use the keys() function in Python. If the dictionary is empty, it produces an empty list. There is no parameter required for this procedure.
- In this example, we will store the key elements in the list by using the dict.keys() method.
Syntax:
Let’s have a look at the syntax and understand the working of dict.keys().
dict.keys()
Note: This method does not take any argument and it will return the list of keys.
Example:
Let’s take an example and check how to define a list of dictionary keys in Python using dict.keys().
# Creation of Dictionary
Country_name = {'U.S.A': 783, 'Germany': 356, 'China': 924}
# Using dict.keys() method
new_output = list(Country_name.keys())
# Display the Content
print("Extract keys from Dictionary :",new_output)
In the above code first, we created a dictionary and assign the elements in the form of key-value pair. To obtain the object dictionary keys, we utilized the dict.keys() method. We assigned dict.keys() as a parameter to the list() constructor. list() returns the list of keys.
Here is the Screenshot of the following given code.
This is how to create or define a list of dictionary keys in Python using dict.keys().
Read: Python list methods
Create a list of dictionary keys in Python using * operator
- Here we will discuss how to construct a list of dictionary keys in Python using * operator.
- The * operator breaks down a sequence. To generate a list, we shall extract the dictionary keys in [].
- To perform this task first, we will create a dictionary and then get keys in a list, we will use the * and the dictionary name in the list.
Example:
Let’s take an example and check how to create a list of dictionary keys in Python using * operator.
Source code:
#Create dictionary
States_in_USA = {
'Colorado':865,
'Georgia':945,
'Kansas':734
}
# Using *operator
new_result = [*States_in_USA]
#print the list
print("Fetched keys from dictionary :", new_result)
In the following given code first, we created a dictionary named “States_in_USA” and assigned the elements in the form of key-value pair. Next, we used the * operator for extracting the keys from the dictionary.
You can refer to the below Screenshot.
In this example we have understood how to create a list of dictionary keys in Python using * operator.
Read: Python Extend Vs Append
How to create a list of dictionary keys in Python using list comprehension
- Now let us see how to create a list of dictionary keys in Python using list comprehension.
- List comprehensions are Python routines that can be used to build new sequences (such as lists, dictionaries, etc.) from existing sequences. They help in reducing larger loops.
- In this example, we will use list comprehension to extract the keys from a dictionary and store them in the result.
Syntax:
Here is the Syntax of the list comprehension method in Python
[expression for element in iterable if condition]
Note:
The list comprehension syntax has three components: an expression, one or more for loops, and, if required, one or more if conditions. Square brackets [must be used around the list comprehension]. The first expression’s outcome will be kept in the new list.
Example:
Here we will take an example and check how to create a list of dictionary keys in Python using list comprehension.
Source code:
#Initialize Dictionary
USA_State_ZipCode = {'Alabama': 35004, 'Alaska': 99501, 'Arizona': 85001}
# Using list comprehension method
new_output = [new_key for new_key in USA_State_ZipCode]
# Display the Content
print("Generated key from dictionary :",new_output)
In the above code first, we created the dictionary named “USA_State_ZipCode” and then used the list comprehension method in which the for loop is used to iterate over the iterable object.
Here is the execution of the following given code.
As you can see in the Screenshot we have discussed How to create a list of dictionary keys in Python using list comprehension.
Read: Concatenate multiple lists in Python
Create a list of dictionary keys in Python using for loop and append()
- In this section, we will discuss how to create a list of dictionary keys in Python using for loop and append().
- In this example, we’ll make a list and add each dictionary key one at a time while iterating through the dictionary’s keys.
- To perform this task first we will define an empty list to contain the keys and append the key during each iteration in for loop.
Example:
Let’s take an example and check how to create a list of dictionary keys in Python using for loop and append().
Source code:
Country_name = {'U.S.A': 783, 'Germany': 356, 'China': 924}
new_lis = []
for new_key in Country_name:
new_lis.append(new_key)
print(new_lis)
In the following given code, we defined the empty list and then used the for loop to iterate the values. And then used the append() function to add the key elements in the empty list.
You can refer to the below screenshot.
This is how to create a list of dictionary keys in Python using for loop and append.
Read: How to add string to list Python
How to create a list of dictionary keys in Python using list()
- Now let us see how to create a list of dictionary keys in Python using list().
- The list () constructor returns a list as its output. It returns an empty list in the case of no arguments. If iterable is assigned as an argument then it will create a list from iterable items.
- In this instance, we’ll create a Dictionary with a few values and then obtain a List of all the keys, which we’ll then store in a variable.
Syntax:
Here is the Syntax of the list() method in Python
list([iterable])
Example:
Let’s take an example and check how to create a list of dictionary keys in Python using list().
Source code:
# Creation of Dictionary
Cars_in_USA = {'BMW': 672, 'Tesla': 834, 'lamborghini': 924}
# Using list() method
new_output = list(Cars_in_USA)
# Display the Content
print("Extracted dictionary keys :",new_output)
In the following code, we first created a dictionary and then used the list() method to extract the key elements from the dictionary.
Here is the Screenshot of the following given code.
You may also like to read the following Python tutorials.
- How to Python Append List to Another List
- How to insert item at end of Python list
- How to get string values from list in Python
- Python List reverse() method [With Examples]
- Python program to select from a list
In this tutorial, we have discussed How to create a list of dictionary keys in Python. we covered five methods for creating a list of dictionary keys in Python: dict.keys(), list comprehension method, *operator, for loop and append(), and list().
- How to create a list of dictionary keys in Python using dict.keys()
- Create a list of dictionary keys in Python using * operator
- How to create a list of dictionary keys in Python using list comprehension
- Create a list of dictionary keys in Python using for loop and append()
- How to create a list of dictionary keys in Python using list()
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.