In this Python tutorial, you will learn how to get dictionary keys as a list using list(), a for loop, list unpacking, and list comprehension. You will also learn how key order, filtering, sorting, and dictionary views affect the result.
Get Python Dictionary Keys as a List
A Python dictionary stores data as key-value pairs. To get its keys as a list, pass the result of dict.keys() to the list() constructor.
keys_list = list(dictionary.keys())
You can also pass the dictionary itself to list(), because iterating over a dictionary returns its keys.
keys_list = list(dictionary)
Both forms create a new list containing the dictionary keys. In modern Python, the keys appear in insertion order.
Difference Between dict.keys() and a List of Keys
The keys() method does not return a list. It returns a dictionary view object of type dict_keys. This view reflects later changes made to the dictionary.
student = {
"name": "Anita",
"course": "Python"
}
keys_view = student.keys()
keys_list = list(student.keys())
student["score"] = 92
print(keys_view)
print(keys_list)
Output
dict_keys(['name', 'course', 'score'])
['name', 'course']
The view includes the newly added score key, while the list remains unchanged because it is a separate object created earlier.
Get Dictionary Keys as a List Using list()
The clearest and most common approach is to convert the object returned by keys() with list().
1. Get keys of a dictionary as a List
In this example, we will create a Dictionary with some initial values and then get all the keys as a List into a variable.
Python Program
#initialize dictionary
aDict = {
'tallest building':'Burj Khalifa',
'longest river':'The Nile',
'biggest ocean':'The Pacific Ocean'
}
# get keys as list
keys = list(aDict.keys())
#print keys
print(keys)
Output
['tallest building', 'longest river', 'biggest ocean']
The variable keys is now a regular Python list. You can access its elements by index, slice it, sort it, or pass it to functions that specifically require a list.
Reference tutorials for the above program
Convert the Dictionary Directly to a List of Keys
Calling list() on a dictionary directly also returns its keys. This works because the default iterator of a dictionary iterates over keys.
employee = {
"id": 101,
"name": "Ravi",
"department": "Sales"
}
keys = list(employee)
print(keys)
Output
['id', 'name', 'department']
list(employee) and list(employee.keys()) produce the same keys. The second form can be more explicit for readers who are new to dictionaries.
Get Dictionary Keys as a List Using a For Loop
2. Get keys of a dictionary as a list using For Loop
In this example, we will create a list and add all the keys of dictionary one by one while iterating through the dictionary keys.
Python Program
#initialize dictionary
aDict = {
'tallest building':'Burj Khalifa',
'longest river':'The Nile',
'biggest ocean':'The Pacific Ocean'
}
# get keys as list
keys = []
for key in aDict.keys():
keys.append(key)
#print keys
print(keys)
Output
['tallest building', 'longest river', 'biggest ocean']
A for loop is more verbose than list(), but it is useful when each key must be checked, transformed, or filtered before being added to the list.
Reference tutorials for the above program
Get Dictionary Keys as a List Using the Unpacking Operator
3. Get keys of a dictionary as a list using * Operator
* operator unpacks a sequence. So, we will unpack the dictionary keys in [], which will create a list.
Python Program
#initialize dictionary
aDict = {
'tallest building':'Burj Khalifa',
'longest river':'The Nile',
'biggest ocean':'The Pacific Ocean'
}
# get keys as list
keys = [*aDict.keys()]
#print the list
print(keys)
Output
['tallest building', 'longest river', 'biggest ocean']
You can also use the dictionary directly instead of dict.keys() as shown below.
Python Program
#initialize dictionary
aDict = {
'tallest building':'Burj Khalifa',
'longest river':'The Nile',
'biggest ocean':'The Pacific Ocean'
}
# get keys as list
keys = [*aDict]
#print the list
print(keys)
Output
['tallest building', 'longest river', 'biggest ocean']
List unpacking is concise, but list(aDict) is usually easier to recognize when the only goal is to convert dictionary keys to a list.
Filter Dictionary Keys into a List
Use a list comprehension when only keys that satisfy a condition should be included.
scores = {
"Anita": 92,
"Ravi": 74,
"Meera": 88,
"Kiran": 61
}
high_scorers = [name for name, score in scores.items() if score >= 80]
print(high_scorers)
Output
['Anita', 'Meera']
The comprehension iterates through both keys and values by using items(), tests each score, and places the matching keys in a new list.
Get Sorted Dictionary Keys as a List
Use sorted() when the keys must be returned in sorted order. The result of sorted() is already a list.
prices = {
"orange": 60,
"apple": 120,
"banana": 50
}
keys = sorted(prices.keys())
print(keys)
Output
['apple', 'banana', 'orange']
For reverse order, pass reverse=True to sorted().
keys = sorted(prices, reverse=True)
print(keys)
Output
['orange', 'banana', 'apple']
Access a Dictionary Key by List Index
A dict_keys view does not support numeric indexing. Convert the keys to a list before accessing a key by position.
settings = {
"theme": "dark",
"language": "English",
"notifications": True
}
keys = list(settings)
print(keys[0])
print(keys[-1])
Output
theme
notifications
Index-based access depends on insertion order. Use the actual key for dictionary lookup whenever possible instead of relying on a key’s position.
Convert Keys from an Empty Dictionary to a List
Converting the keys of an empty dictionary produces an empty list.
data = {}
keys = list(data.keys())
print(keys)
Output
[]
Choose the Right Method for Dictionary Keys
- Use
list(dictionary)orlist(dictionary.keys())to create a normal list of every key. - Use
dictionary.keys()when a live dictionary view is sufficient and list operations are not required. - Use
sorted(dictionary)to get a sorted list of keys. - Use a list comprehension to filter or transform keys.
- Use a for loop when the conversion requires several statements or additional processing.
- Use
[*dictionary]when list unpacking fits the surrounding code.
Common Issues When Converting Dictionary Keys to a List
- Expecting keys() to return a list: it returns a
dict_keysview. Wrap it withlist()when list behavior is needed. - Trying to index dict_keys: expressions such as
dictionary.keys()[0]fail because dictionary views are not subscriptable. - Expecting values instead of keys:
list(dictionary)returns keys. Uselist(dictionary.values())for values. - Expecting key-value pairs: use
list(dictionary.items())to create a list of key-value tuples. - Assuming alphabetical order: dictionary iteration follows insertion order, not sorted order. Use
sorted()when ordering is required.
Python Dictionary Keys as a List FAQs
What is the simplest way to get dictionary keys as a list?
Use list(dictionary) or list(dictionary.keys()). Both return a new list containing all keys.
Does dict.keys() return a Python list?
No. It returns a dynamic dict_keys view. Convert it with list(dictionary.keys()) when a list is required.
How do I get the first key from a dictionary?
You can use next(iter(dictionary)) without creating a complete list. When a list is already needed, use list(dictionary)[0]. Both approaches require the dictionary to contain at least one item.
How do I get dictionary keys in alphabetical order?
Use sorted(dictionary). It returns the keys as a sorted list.
Summary of Getting Python Dictionary Keys as a List
In this Python Tutorial, we learned how to get the keys of a dictionary as a Python list using list(), a for loop, and the unpacking operator. We also covered dictionary views, insertion order, filtering, sorting, indexing, and common conversion errors.
TutorialKart.com