Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. Dictionaries in python
  5. Functions and Methods of Dictionary

Functions and Methods of Dictionary

Let us now talk about various built-in functions and methods provided by python to manipulate python dictionaries.

  • items()– This method returns a list of tuples with each tuple containing one key and the corresponding values.
    e.g.,
a={'1' :'python', '2' :'Java','3' :'JavaScript','4':'C', '5':'C++'}
a.items()
Output-dict_items([('1', 'python'), ('2', 'Java'), ('3', 'JavaScript'), ('4', 'C'), ('5', 'C++')])
  • keys()– This method returns a list object comprising keys in the dictionary.
    e.g.,
a={'1' :'python', '2' :'Java','3' :'JavaScript','4':'C', '5':'C++'}
a.keys()
Output-dict_keys['1', '2', '3', '4', '5']
  • values()– This method returns a list object comprising values in the dictionary.
    e.g.,
a={'1' :'python', '2' :'Java','3' :'JavaScript','4':'C', '5':'C++'}
a.values()
Output- {'python','Java','JavaScript','C','C++')
  • get()- With this method, we can get the items with the given key, similar to dictionary[key].
    e.g.,
a={'1' :'python', '2' :'Java','3' :'JavaScript','4':'C', '5':'C++'}
a.get('4')
Output- 'C'

Was this article helpful to you? Yes No

How can we help?