A dictionary in Python stores data in key–value pairs, and often you may need to extract all keys from it — for iteration, display, or processing.
Python provides multiple ways to get a list of all dictionary keys depending on what you need (view object, list, loop, etc.).
🧩 Example Dictionary
student = {
"name": "Dheeraj",
"age": 24,
"course": "Cybersecurity",
"city": "Delhi"
}🔹 1. Using dict.keys() Method (Recommended)
The simplest and most common way is to use the .keys() method, which returns a view object containing all keys.
keys = student.keys()
print(keys)Output:
dict_keys(['name', 'age', 'course', 'city'])✅ The dict_keys object behaves like a set view — it’s iterable, but not a list.
🔹 2. Convert Keys to a List
If you need an actual list (for indexing, sorting, etc.), wrap it with list().
keys_list = list(student.keys())
print(keys_list)Output:
['name', 'age', 'course', 'city']✅ Use this when you need to manipulate keys (e.g., sort, slice, etc.).
🔹 3. Using a For Loop
You can directly iterate through the dictionary (iterating a dictionary by default gives you keys).
for key in student:
print(key)Output:
name
age
course
city✅ Use this when you only need to loop over keys without creating a new list.
🔹 4. Using Dictionary Unpacking (*dict)
You can use the * operator to unpack all keys.
keys = [*student]
print(keys)Output:
['name', 'age', 'course', 'city']✅ This is a short and clean trick to quickly convert keys into a list.
🔹 5. Using List Comprehension
You can extract keys manually with list comprehension (useful when adding conditions).
keys = [key for key in student]
print(keys)Output:
['name', 'age', 'course', 'city']✅ Works well if you want to filter keys dynamically.
Example:
filtered_keys = [key for key in student if len(key) > 3]
print(filtered_keys)Output:
['name', 'course', 'city']🔹 6. Using map() Function
keys = list(map(str, student.keys()))
print(keys)Output:
['name', 'age', 'course', 'city']✅ Rarely used, but useful if you want to transform key names (e.g., convert to uppercase).
🔹 7. Using sorted() to Get Keys in Order
If you need the keys sorted alphabetically:
keys = sorted(student.keys())
print(keys)Output:
['age', 'city', 'course', 'name']🧾 Summary Table
| Method | Description | Returns |
|---|---|---|
dict.keys() | Returns a view of keys | dict_keys([...]) |
list(dict.keys()) | Converts keys to list | ['key1', 'key2'] |
for key in dict | Loops through keys | Prints keys |
[key for key in dict] | List comprehension | ['key1', 'key2'] |
[*dict] | Unpacks keys | ['key1', 'key2'] |
sorted(dict.keys()) | Sorted list of keys | ['key1', 'key2'] |
✅ Best Practice
- Use
list(dictionary.keys())if you need an editable list. - Use
dictionary.keys()if you just need to iterate or view keys (memory-efficient). - Use list comprehension if you need filtering or conditional logic.
Final Example
student = {"name": "Dheeraj", "age": 24, "course": "Cybersecurity"}
print(student.keys()) # dict_keys(['name', 'age', 'course'])
print(list(student.keys())) # ['name', 'age', 'course']
print([key for key in student]) # ['name', 'age', 'course']
print(sorted(student.keys())) # ['age', 'course', 'name']✅ In short:
To get all keys from a dictionary, use
dictionary.keys(),
and if you need a list — simply wrap it inlist().
