33 - Dictionary in python

Python Dictionaries

Dictionaries (aka associative array) 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}
Info
  • we can also write dictionaries like this:
 info = {
   'name': 'Karan',
   'age': 19,
   'eligible': True
}
  • This can be used to make code more user-friendly.

Accessing Dictionary items

get():

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

Ex:

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

print(info['name'])
print(info.get('eligible')) # i use this one


#Output:
Karan
True

values():

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

Ex:

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

#Output:
dict_values(['Karan', 19, True])

keys():

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

Ex:

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


#Output:
dict_keys(['name', 'age', 'eligible'])

items():

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

Ex:

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


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

Removing items from dictionary

clear():

The clear() method Removes all the items from the dictionary.

Ex:

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

# Output:
{}

pop():

The pop() method removes the key-value pair whose key is passed as a parameter.

Ex:

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


#Output:
{'name': 'Karan', 'age': 19}

del:

we can also use the del keyword to remove a dictionary item.

  • If key is not provided, then the del keyword will delete the dictionary entirely.

Ex:

info = {'name':'Karan', 'age':19, 'eligible':True, 'DOB':2003}
del info['age']
print(info)

del info
print(info)


#Output:
{'name': 'Karan', 'eligible': True, 'DOB': 2003}

NameError: name 'info' is not defined

popitem():

The popitem() method removes the last key-value pair from the dictionary.

Ex:

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


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

Others

update()

The update() method updates the value of the key provided to it,

  • If the item already exists in the dictionary,
  • Else it creates a new key-value pair.

Ex:

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

info.update({'age':20})
print(info)


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

{'name': 'Karan', 'age': 20, 'eligible': True, 'DOB': 2001}

Copy():

Using this method, we can copy a dictionary.

Ex

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

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

Loops in Dictionary

We can use loop with Dictionaries with .item()

Ex:

Dict = {'name':'Karan', 'age':19, 'eligible':True, 'DOB':2003}

for k,v in Dict.items():  # k,v represents keys, values.
	print(f'{k}:',v)


# Output:
name: Karan
age: 19
eligible: True
DOB: 2003