Skip to main content

Command Palette

Search for a command to run...

Day-33 Dictionary

Updated
2 min read
Day-33 Dictionary
C
I am Data Science student, has a little bit knowledge on Web Development. I also love writing and editing as my hobby. Passionate to explore the world.

Python Dictionaries

Dictionaries are ordered collection of data items. They store multiple items in a single variable. Dictionary items are key-value pairs that are separated by commas and enclosed within curly brackets {}.

Example:

info = {'name':'Karan', 'age':19, 'eligible':True}
print(info)

Output:

{'name': 'Karan', 'age': 19, 'eligible': True}

Accessing Dictionary items:

I. Accessing single values:

Values in a dictionary can be accessed using keys. We can access dictionary values by mentioning keys either in square brackets or by using get method.

Example:

info = {'name':'Karan', 'age':19, 'eligible':True}
print(info['name'])
print(info.get('eligible'))

Output:

Karan
True

II. Accessing multiple values:

We can print all the values in the dictionary using values() method.

Example:

info = {'name':'Karan', 'age':19, 'eligible':True}
print(info.values())

Output:

dict_values(['Karan', 19, True])

III. Accessing keys:

We can print all the keys in the dictionary using keys() method.

Example:

info = {'name':'Karan', 'age':19, 'eligible':True}
print(info.keys())

Output:

dict_keys(['name', 'age', 'eligible'])

IV. Accessing key-value pairs:

We can print all the key-value pairs in the dictionary using items() method.

Example:

info = {'name':'Karan', 'age':19, 'eligible':True}
print(info.items())

Output:

dict_items([('name', 'Karan'), ('age', 19), ('eligible', True)])

Practice

info = {'name':'Karan', 'age':19, 'eligible':True}
# print(info) 
# print(info.keys())
# print(info.values())

# for key in info.keys():
#   print(f"The value corresponding to the key {key} is {info[key]}")

print(info.items())
for key, value in info.items():
  print(f"The value corresponding to the key {key} is {value}") 

Output:

dict_items([('name', 'Karan'), ('age', 19), ('eligible', True)])

The value corresponding to the key name is Karan
The value corresponding to the key age is 19
The value corresponding to the key eligible is True

Explanation:

  • info = {'name':'Karan', 'age':19, 'eligible':True}

    • Creates a dictionary with key–value pairs.
  • print(info.items())

    • Returns all key-value pairs as dictionary view objects.

    • Output format:

      dict_items([('name', 'Karan'), ('age', 19), ('eligible', True)])
      
  • for key, value in info.items():

    • Loops through each key-value pair in the dictionary.
  • print(f"The value corresponding to the key {key} is {value}")

    • Prints each key with its corresponding value.

100DaysofPython

Part 34 of 35

This series is for beginners in which we explore python language along with how it is used in data science and do some exercises and some python related projects.

Up next

Day34-Dictionary Methods

Dictionary Methods Dictionary uses several built-in methods for manipulation. They are listed below: i. update() The update() method updates the value of the key provided to it if the item already exi