AppDividend
Latest Code Tutorials

Python Dictionary Keys Example | Keys() Method Tutorial

0 19

Get real time updates directly on you device, subscribe now.

Python Dictionary Keys Example | Keys() Method Tutorial is today’s topic. The dictionary method keys() returns the list of all the available keys in the dictionary. The keys() method returns the view object. That view object contains keys of the dictionary, as a list.

Python Dictionary Keys Example

The keys() doesn’t take any parameters. When the dictionary is changed, the view object also reflects these changes. The syntax for keys() method is following.

dictionary.keys()

Let see the following example.

# app.py

dict = { 
    'shopping': 'flipkart',
    'transport': 'ola',
    'banking': 'paytm',
    'hotel': 'oyo rooms'
 }
dictKeys = dict.keys()
print(dictKeys)

The output is following.

Python Dictionary Keys Example | Keys() Method Tutorial

Now, if you emptied the dictionary, then the keys also get emptied.

# app.py

dict = { 
    'shopping': 'flipkart',
    'transport': 'ola',
    'banking': 'paytm',
    'hotel': 'oyo rooms'
 }
dictKeys = dict.keys()
print(dictKeys)

dict = {}
dictKeys = dict.keys()
print(dictKeys)

See the output below.

Python Dictionary Keys Example Tutorial

The keys() can be used to access the elements of the dictionary as we can do for the list, without the use of keys(), no other mechanism provides means to access dictionary keys as the list by index.

Finally, Python Dictionary Keys Example Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.