In this Python tutorial, we’ll learn what Python dictionary keys method is and how to use it properly inside Python code programs. We’ll walk through various Python code examples in order to practically understand the use of keys() method.
Introduction: Python Dictionary Keys Method
This method is used to take all the keys of specified dictionary and return a list of these keys as a view object.
Let’s understand it with the help of code examples. But first, let’s walk through its syntax.
Syntax of keys()
dictionary.keys()
- We can see that this Python dictionary keys method does not take any arguments.
- This method returns a view object that contains list of keys of its calling dictionary. For instance, if the keys() method returns dict_keys([‘a’,’b’,’c’]), then in this case dict_keys() is a view object and [‘a’,’b’,’c’] is a list of keys.
- Applying keys() method on an empty Python dictionary will result in returning a view object which contains an empty list.
Implementing Python Dictionary Keys Method (Example Code Examples)
Let’s now implement this method inside practical Python code examples.
Example 1: keys() applied on Dictionary
dictVal = {'name':'Zeeshan','height':6,'colorOfEyes':'Brown'} print( dictVal.keys() )
Output
dict_keys(['name', 'height', 'colorOfEyes'])
- In this code example, we’ve created a dictionary with some key value pairs.
- After that, we’ve called key() method of this dictionary and have used it inside print function to display the value returned by it.
- We can see that it has returned a view object(dict_keys()) which contains a list of keys. This method take only the keys from the specified Python dictionary.
Example 2: Empty Dictionary
a = {} print( a.keys() )
Output
dict_keys([])
- In this example, first we’ve created an empty dictionary.
- Then we’ve applied keys() method on it. As a result, it returned a view object which contains an empty list.
Example 3: Return Value of Python Dictionary Keys Method
a = {'a':234,'b':4,'c':[4,5]} returnedValue = a.keys() print( returnedValue )
Output
dict_keys(['a', 'b', 'c'])
- In this code example, we’ve created a dictionary.
- After that, we’ve called its key method and have stored the returned value inside a variable.
- Finally, we’ve displayed value of this variable.
- It shows that keys() method returns a view object which contains a list of keys of the specified dictionary.
Example 4: Apply keys() on List of Dictionaries using Python While Loop
listOfDicts=[{'a':1,'b':2},{23:167,4.7:27},{'color':'Red'}] newList=[] i=0 while i<len(listOfDicts): newList.append(listOfDicts[i].keys()) i+=1 print(newList)
Output
[dict_keys(['a', 'b']), dict_keys([23, 4.7]), dict_keys(['color'])]
- In this code, we’ve first specified a Python list of dictionaries. We’ve also created an empty list that will be used to store the resulting values.
- Also, we’ve created an integer and have passed 0 as value to it. Its been used to execute the while loop until value of this integer reaches the length of list. In order to do that, we’ve incremented value of this integer by 1 in every iteration.
- Inside the body of while loop, we’ve applied keys method on items(dictionaries) of list by using the index value of items and have added them to new list using append method.
- Finally, we’ve printed the list and it shows view objects containing lists of specified dictionary keys.
So this this how we can apply keys method on list of Python dictionaries and store the result in new list.
Example 5: apply keys() method on Python List of Dictionaries using For Loop
listOfDicts=[{'aa':12,'bb':22},{1:167,2:27},{'hobby':'Soccer'}] newListVals=[] for var in listOfDicts: newListVals.append(var.keys()) print(newListVals)
Output
[ dict_keys(['aa', 'bb']), dict_keys([1, 2]), dict_keys(['hobby']) ]
- In this example code, we’ve first specified a Python list of dictionaries.
- After that, we’ve used Python for loop which will take each item(dictionary) from the specified list while iterating over it. Click here to learn more about iterating over multiple Python lists simultaneously.
- In the body of for loop, we’ve used the keys method of the items(dictionaries) of list. We also have added the resulting values to new list using its append method.
- Finally, we’ve displayed values of the new list. In the above output section, we can see that it displayed view objects which contains lists of specified dictionary keys.
Example 6: Updating Dictionary also Updates the View Object
dic={'a':1,'b':2,'c':3} result=dic.keys() print(result) dic.update({'d':4}) print(result)
Output
dict_keys(['a', 'b', 'c']) dict_keys(['a', 'b', 'c', 'd'])
In this code, we’ve created a simple dictionary and have applied keys method on it. Then we’ve added a new key, value pair to the dictionary and we can see that the the list of keys of view object also gets updated.
We’ll be very happy if by means of this article, you have gained knowledge of Python dictionary keys method. We would love to see you share it with other Python programmers.
Feel free to ask if you still have queries regarding Python dictionary keys method. We’ll be very glad to help you solve all your queries.
Conclusion
To conclude this article, we hope you now have a grasp of the knowledge of how to make use of the Python dictionary keys method. Do feel free to leave your informative feedback in the comment section.
Other worth reading posts on Python programming are also posted on this site. Feel free to pay a visit to them as well. Thank you for reading this article.