How does Python dict.keys() Return a List and a Set?
Last Updated :
11 Mar, 2024
Here, we have tasks to identify how Python dict.keys() return a list and set and print as a result. In this article, we will some examples of how Python dict.keys() return a list and a set.
Example :
Input : {'a': 1, 'b': 2, 'c': 3} , <class 'dict_keys'>
Output : <class 'list'>
<class 'set'>
Explanation : Here, we have dict_keys() and we return it as list and set datatype.
How does Python dict.keys() Return a List and a Set?
Below are examples of how Python dict.keys() return a list and set in Python and not a key view object:
- dict.keys() Return a List
dict.keys()
Returns a Set
Basic Example
In this example, in the below code, a dictionary (`the `) is created, and its keys are obtained using `dict.keys()`. The keys are then displayed using a `print` statement, showcasing the key’s view object.
Python3
sample_dict = { 'a' : 1 , 'b' : 2 , 'c' : 3 }
keys_view = sample_dict.keys()
print ( "Keys:" , keys_view)
print ( type (keys_view))
|
Output
Keys: dict_keys(['a', 'b', 'c'])
<class 'dict_keys'>
How dict.keys() Return a List
Below are some of the examples by which dict.keys() returns a list:
Example 1: Convert to List Explicitly
In this example, in below code, a sample dictionary (`sample_dict`) is created with key-value pairs. The `dict.keys()` method is then used to obtain a view object (`keys_view`) representing the keys. Subsequently, the keys are explicitly converted to a list (`keys_list`).
Python3
sample_dict = { 'a' : 1 , 'b' : 2 , 'c' : 3 }
keys_view = sample_dict.keys()
keys_list = list (sample_dict.keys())
print ( type (keys_view))
print ( "Keys as List:" , keys_list)
print ( type (keys_list))
|
Output
<class 'dict_keys'>
Keys as List: ['a', 'b', 'c']
<class 'list'>
Example 2: Iterate Over Keys as List
In this example, in below code, a dictionary named `sample_dict` is created with key-value pairs. The `dict.keys()` method is used to obtain a view object (`keys_view`) representing the keys, which is then iterated over in a `for` loop. The keys are subsequently converted into a list (`keys_list`).
Python3
sample_dict = { 'a' : 1 , 'b' : 2 , 'c' : 3 }
keys_view = sample_dict.keys()
for key in keys_view:
print ( "Key:" , key)
keys_list = list (keys_view)
print ( type (keys_view))
print ( "Keys as List:" , keys_list)
print ( type (keys_list))
|
Output
Key: a
Key: b
Key: c
<class 'dict_keys'>
Keys as List: ['a', 'b', 'c']
<class 'list'>
How dict.keys()
Returns a Set
Below are some of the examples by which dict.keys() returns a set:
Example 1: Converting to a Set Explicitly
In this example, in below code, a dictionary (`sample_dict`) is created, and its keys are converted into a set (`keys_set`). The code then prints the type of the original dictionary, displays the set of keys, and prints the type of the resulting set.
Python3
sample_dict = { 'a' : 1 , 'b' : 2 , 'c' : 3 }
keys_set = set (sample_dict.keys())
print ( type (sample_dict))
print ( "Keys as Set:" , keys_set)
print ( type (keys_set))
|
Output
<class 'dict'>
Keys as Set: {'a', 'c', 'b'}
<class 'set'>
Share your thoughts in the comments
Please Login to comment...