The keys()
method in Python is a built-in method that returns a list of the dictionary’s keys. This method is commonly used when working with dictionaries in Python to iterate over the keys or to create a new list of keys.
Syntax
dict.keys()
Parameters
The keys()
method does not take any parameters. It simply returns a list of the dictionary’s keys.
Examples
Example 1: Iterating Over the Dictionary’s Keys
Consider the following example, where we iterate over the keys of a dictionary:
student = {'name': 'John', 'age': 25, 'gender': 'Male'} for key in student.keys(): print(key)
In this example, we have created a dictionary called student
, which contains the key-value pairs of a student. Then, we use a for loop to iterate over the keys of the dictionary using the keys()
method. Finally, we print the key of each iteration.
Example 2: Converting a Dictionary to a List of Keys
Consider the following example, where we convert a dictionary to a list of keys:
student = {'name': 'John', 'age': 25, 'gender': 'Male'} student_keys = list(student.keys()) print(student_keys)
In this example, we have created a dictionary called student
, which contains the key-value pairs of a student. Then, we use the list()
function to convert the keys of the dictionary to a list using the keys()
method. Finally, we print the resulting list, which will be:
['name', 'age', 'gender']
Example 3: Checking if a Key Exists in a Dictionary
Consider the following example, where we check if a key exists in a dictionary:
student = {'name': 'John', 'age': 25, 'gender': 'Male'} key = 'name' if key in student.keys(): print(f"The key '{key}' exists in the dictionary.") else: print(f"The key '{key}' does not exist in the dictionary.")
In this example, we have created a dictionary called student
, which contains the key-value pairs of a student. Then, we have a variable called key
which we want to check if it exists in the dictionary. Finally, we use the in
operator along with the keys()
method to check if the key exists in the dictionary. If the key exists, we print a message indicating that the key exists. If the key does not exist, we print a message indicating that the key does not exist.
Return Value
The keys()
method returns a list of all the keys in the dictionary. The list is dynamic, meaning that if you add or remove elements from the dictionary, the list will reflect these changes.
Notes
It is important to note that dictionaries in Python are unordered, meaning that the order in which the keys are added to the dictionary is not guaranteed. However, starting from Python 3.7, dictionaries are guaranteed to maintain the order in which items were inserted. If you need to maintain the order of the keys, you can use the collections
module’s OrderedDict
class.
In Python 2, the keys()
method returns a list of keys, but in Python 3, it returns a dict_keys object, which is an iterable view of the dictionary’s keys. To get a list in Python 3, you can pass the result of the keys()
method to the list()
function, just like in the second example above.
Additionally, if you need to get both the keys and values of a dictionary, you can use the items()
method, which returns a list of tuples, where each tuple contains a key-value pair from the dictionary.