Python Dictionary keys() Method With Examples

Python Dictionary keys

Now, we are going to have a look at the keys method. The keys method is going to give us a view object of the keys of the dictionary. The object contains the keys of the dictionary. Let’s have a look at a sample program, which would help us better understand the keys method, and also help us visualize the output.

Python Dictionary keys() Method

As you can see, in the program, we are having a dictionary, and then we have called the keys method here. I have directly printed the output over here since we are more interested in the output right now.

As an output, we get a view object, which contains all the keys of the dictionary, as a list. The output looks something like this –

dict_keys([‘one’, ‘two’, ‘three’])

As you can see in the output, we got the object with the keys for the given dictionary. So, we can make use of the keys method from the dictionary, as and when required.