In Python 2.x version, this was easy because the function dict.keys() by default returned a list of keys of the dictionary but that is not the case with Python 3.x version.
In Python 3.x if we use this code, the dict.keys()
function returns a dictionary view object which acts as a set.
newdict = {1:0, 2:0, 3:0}
print(newdict.keys())
Output:
dict_keys([1, 2, 3])
So this won't work in Python 3.x version but there are many other ways to get dictionary keys in a list.
Directly convert Dictionary to List
By default, iterating over a Python dictionary returns the dictionary keys, hence if we use the list()
method with the dictionary object we will get a list of dictionary keys. For example,
newdict = {1:0, 2:0, 3:0}
print(list(newdict))
Output:
[1, 2, 3]
Convert the Dictionary Keys to List
We can use the dict.keys()
method to get the dictionary view object for the list, but then we can use the list() function to directly convert it into a list.
newdict = {1:0, 2:0, 3:0}
print(list(newdict.keys()))
Output:
[1, 2, 3]
Using the Unpacking Operator
The unpacking operator with *
works with any object that is iterable and, since dictionaries return their keys when iterated through, you can easily create a list by using it within a list literal.
newdict = {1:0, 2:0, 3:0}
print([*newdict])
Output:
[1, 2, 3]
Adding .keys()
i.e [*newdict.keys()]
might help in making your intent a bit more explicit though it will cost you a function look-up and invocation. (which, in all honesty, isn't something you should really be worried about).
Using List Comprehension
Well although the different ways listed above would be enough, let's give you one more option which is using the list comprehension technique in which we will be using the dict.keys()
function too.
newdict = {1:0, 2:0, 3:0}
keys = newdict.keys()
print([k for k in keys])
Output:
[1, 2, 3]
Conclusion
Accessing dictionary keys as a list provides a flexible and convenient way to work with key-related operations in Python. Through the methods explored in this article, you've discovered multiple approaches to extracting keys and transforming them into a list.
By leveraging techniques such as dict.keys(), list(dict), or list comprehensions, you can effortlessly obtain a list of dictionary keys.
So now you know 4 ways of getting dictionary keys in the form of a list. Python has the advantage that one task can be accomplished by many simple techniques. If you know any other technique then do share it with us in the comments.
Frequently Asked Questions(FAQs)
1. How can I get the keys from a dictionary as a list in Python?
There are multiple methods to achieve this. You can use dict.keys() to obtain a view object and convert it to a list using list(dict.keys()). Alternatively, you can use list comprehensions, such as [key for key in dict] or list(dict).
2. Are dictionary keys returned as a list by default in Python?
No, dictionary keys are not returned as a list by default. Instead, they are returned as a view object, which provides a dynamic and iterable representation of the keys.
3. Can I modify the list of keys obtained from a dictionary?
Yes, if you convert the dictionary keys to a list using one of the methods mentioned earlier, you can freely modify the resulting list. However, any changes made to the list will not affect the original dictionary.
4. Is the order of dictionary keys preserved when converting them to a list?
Starting from Python 3.7, the order of dictionary keys is guaranteed to be insertion-ordered, meaning it will be preserved when converting the keys to a list. However, for versions prior to Python 3.7, the order was not guaranteed.
5. How can I check if a specific key exists in a dictionary using the extracted keys list?
Once you have the dictionary keys as a list, you can use the "in" operator to check if a specific key exists in the list. For example, "key_to_check in list_of_keys" will return True if the key is present and False otherwise.
You may also like: