What Is Dictionary In Python

 What Is Dictionary In Python

  • Dictionary is likely the most important built-in Python data structure.

  • Dictionaries store a mapping between a set of keys and a set of values.

  • Keys are variables, and they are listed together with the values assigned to them. This forms a key-value pair.

  • The keys can be of any immutable type and the values can be of any type.

  • A dictionary is a flexibly-sized collection of key-value pairs, where keys and values are Python objects.


How To Store Data In Dictionary In Python

dict1 = {'Name' : "Zara", "Age" : 7, "Class" : "First"}
dict1 

Output:

{'Name': 'Zara', 'Age': 7, 'Class': 'First'}


How to fetch the column names in Dictionary In Python


dict1.keys()

Output:

dict_keys(['Name', 'Age', 'Class'])

How To Fetch Values From Dictionary In Python

dict1.values()

Output:

dict_values(['Zara', 7, 'First'])

How To Fetch The Key Value Pair From Dictionary In Python

dict1.items()


Output:

dict_items([('Name', 'Zara'), ('Age', 7), ('Class', 'First')])

How To Access The Values In Dictionary In Python

The Python Dictionary object provides a key:value indexing facility.

Note that dictionaries are unordered - since the values in the dictionary are indexed by keys, they are not held in any particular order, unlike a list, where each item can be located by its position in the list.

How To Access The Values In Dictionary By the Keys

print (dict1['Name'])
print ()
print (dict1["Age"])

Output:

Zara

7

Here we have accessed the values by the keys like Name and Age


How To Update Dictionary

dict1 = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print (dict1)
print ()

print (dict1['Age'])
print ()

dict1['Age'] = [7,8] # updating/ modifying a value
print (dict1)
print ()

dict1['School'] = 'DPS' # adding a key value pair
print (dict1)

Output:

{'Name': 'Zara', 'Age': 7, 'Class': 'First'}


7


{'Name': 'Zara', 'Age': [7, 8], 'Class': 'First'}


{'Name': 'Zara', 'Age': [7, 8], 'Class': 'First', 'School': 'DPS'}


How To Delete Key Value Pair In Dictionary In Python

print (dict1)
print ()
print (dict1["School"])

print ()
del dict1["School"]
print (dict1)

Output:


We have deleted School column and it's value from the dictionary

{'Name': 'Zara', 'Age': [7, 8], 'Class': 'First', 'School': 'DPS'}

DPS

{'Name': 'Zara', 'Age': [7, 8], 'Class': 'First'}

How to clear Dictionary

dict1.clear()
print (dict1) # empty the dictionary

Output:

{}

How To Delete Dictionary

del (dict1)
print (dict1)


Output:

It will throw error dictonary does not exist

How To Remove Item From Dictionary

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict.pop("model")
print(thisdict)

Output:

{'brand': 'Ford', 'year': 1964}

How To Print Key Values In Dictionary Using For Loop In Python

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
for x in thisdict:
  print(x)

Output:

brand
model
year

The keys() method also can be used to return the keys of a dictionary:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
for x in thisdict.keys():
  print(x)

Output:

brand
model
year

item() method can be used to fetch key value pair from the Dictionary

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
for x, y in thisdict.items():
  print(x, y)


Output:

brand Ford
model Mustang
year 1964





How To Print Values In Dictionary Using For Loop In Python

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
for x in thisdict:
  print(thisdict[x])

Output:

Ford
Mustang
1964


You can also use the values() method also to return values of a dictionary:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
for x in thisdict.values():
  print(x)

Output:

Ford
Mustang
1964

















Comments