0% found this document useful (0 votes)
17 views8 pages

Python Dictionaries

Uploaded by

btlgndz02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views8 pages

Python Dictionaries

Uploaded by

btlgndz02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Python Dictionaries

Dictionaries are used to store data values in key:value pairs.Dictionaries are created by enclosing
a comma-separated list of key-value pairs in curly braces { }.

# Create a dictionary of boy details


boy = {'Name': 'Ali', 'Age': 21, 'Hieght': 6, 'Weight': 68, 'City':
'Peshawar', 'Religion': 'Muslim'}
print(boy)

{'Name': 'Ali', 'Age': 21, 'Hieght': 6, 'Weight': 68, 'City':


'Peshawar', 'Religion': 'Muslim'}

Dictionary Items
Dictionary items are ordered, muteable(changeable), and does not allow duplicates.

# Create a dictionary to show dictionary items are ordered and can not
allow duplicate
boy = {'Name': 'Ali', 'Age': 21, 'Hieght': 6, 'Weight': 68, 'City':
'Islamabad', 'City': 'Peshawar', 'Religion': 'Muslim'}
print(boy)

{'Name': 'Ali', 'Age': 21, 'Hieght': 6, 'Weight': 68, 'City':


'Peshawar', 'Religion': 'Muslim'}

# change the value in the dictionary


boy['Age'] = 23

print(boy)

{'Name': 'Ali', 'Age': 23, 'Hieght': 6, 'Weight': 68, 'City':


'Peshawar', 'Religion': 'Muslim'}

# The update() method is also used to change the the value in the
dictionary
boy.update({'Age': 22})

print(boy)

{'Name': 'Ali', 'Age': 22, 'Hieght': 6, 'Weight': 68, 'City':


'Peshawar', 'Religion': 'Muslim'}

Dictionary Length
# To determine how many items dictionary have, use the len() function
print(len(boy))
6

Accessing dictionary Items


# Access the items of a dictionary by referring to its key name,
inside square brackets
boy = {'Name': 'Ali', 'Age': 21, 'Hieght': 6, 'Weight': 68, 'City':
'Islamabad', 'City': 'Peshawar', 'Religion': 'Muslim'}

# Access the city


city = boy['City']
print(city)

Peshawar

# There is also a method called get() that will give you the same
result
religion = boy.get('Religion')
print(religion)

Muslim

Add Dictionary Items


# Adding an item to the dictionary is done by using a new index key
and assigning a value to it
boy = {'Name': 'Ali', 'Age': 21, 'Hight': 6.3, 'Weight': 68, 'City':
'Islamabad', 'City': 'Peshawar'}

# add religion to the dictionary


boy['Religion'] = 'Muslim' # This will add the
'Religion' key-value pair to the end of the dictionary

print(boy)

{'Name': 'Ali', 'Age': 21, 'Hight': 6.3, 'Weight': 68, 'City':


'Peshawar', 'Religion': 'Muslim'}

Python Dictionary Methods


1.Update
# The update() method will update the dictionary with the items from a
given argument. If the item does not exist, the item will be added to
the dictionary.
boy = {'Name': 'Ali', 'Age': 21, 'Hight': 6.3, 'Weight': 68, 'City':
'Islamabad', 'City': 'Peshawar'}

# Updated the dictionary with the Religion as key and Muslim as value
boy.update({'Religion': 'Muslim'})

print(boy)

{'Name': 'Ali', 'Age': 21, 'Hight': 6.3, 'Weight': 68, 'City':


'Peshawar', 'Religion': 'Muslim'}

Remove Dictionary Items


Del
# The del keyword removes the item with the specified key name
boy = {'Name': 'Ali', 'Age': 21, 'Hieght': 6, 'Weight': 68, 'City':
'Peshawar', 'Religion': 'Muslim'}

# delete any item from the dictionary


del boy['Hieght']

print(boy)

{'Name': 'Ali', 'Age': 21, 'Weight': 68, 'City': 'Peshawar',


'Religion': 'Muslim'}

# The del keyword can also delete the dictionary completely


boy = {'Name': 'Ali', 'Age': 21, 'Hieght': 6, 'Weight': 68, 'City':
'Peshawar', 'Religion': 'Muslim'}

# delete the dictionary


del boy

print(boy) # This will cause an error because


the dictionary boy no longer exists

----------------------------------------------------------------------
-----
NameError Traceback (most recent call
last)
Cell In[63], line 7
4 # delete the dictionary
5 del boy
----> 7 print(boy)

NameError: name 'boy' is not defined


2. Pop
# the pop() method removes the item with the specified key name
boy = {'Name': 'Ali', 'Age': 21, 'Hieght': 6, 'Weight': 68, 'City':
'Peshawar', 'Religion': 'Muslim'}

# remove any item from the dictionary


boy.pop('Weight')

print(boy)

{'Name': 'Ali', 'Age': 21, 'Hieght': 6, 'City': 'Peshawar',


'Religion': 'Muslim'}

3. Popitem
# The popitem() method removes the last inserted item
boy = {'Name': 'Ali', 'Age': 21, 'Hieght': 6, 'Weight': 68, 'City':
'Peshawar', 'Religion': 'Muslim'}

# remove the last item of the dictionary


boy.popitem()

print(boy)

{'Name': 'Ali', 'Age': 21, 'Hieght': 6, 'Weight': 68, 'City':


'Peshawar'}

4.Clear
# The clear() method empties the dictionary
boy = {'Name': 'Ali', 'Age': 21, 'Hieght': 6, 'Weight': 68, 'City':
'Peshawar', 'Religion': 'Muslim'}

# Empty the dictionary


boy.clear()

print(boy)

{}

5. Copy
# The copy() method returns a copy of the specified dictionary
boy = {'Name': 'Ali', 'Age': 21, 'Hieght': 6, 'Weight': 68, 'City':
'Peshawar', 'Religion': 'Muslim'}

# copy the boy dictionary


boy_new = boy.copy()
print(boy_new)

{'Name': 'Ali', 'Age': 21, 'Hieght': 6, 'Weight': 68, 'City':


'Peshawar', 'Religion': 'Muslim'}

6. Get
# The get() method returns the value of the item with the specified
key
boy = {'Name': 'Ali', 'Age': 21, 'Hieght': 6.7, 'Weight': 69, 'City':
'Peshawar', 'Religion': "Muslim"}

# get the value of the item


city = boy.get('City')

print(city)

Peshawar

# Try to return the value of an item that do not exist


education = boy.get('education','Undergraduate')

print(education)

Undergraduate

7. Keys
# The keys() method returns the keys of the dictionary as a list
boy = {'Name': 'Ali', 'Age': 21, 'Hieght': 6.7, 'Weight': 69, 'City':
'Peshawar', 'Religion': "Muslim"}

# Get the keys of the dictionary


dict_keys = boy.keys()

print(dict_keys)

dict_keys(['Name', 'Age', 'Hieght', 'Weight', 'City', 'Religion'])

8. Values
# The values() method returns the values of the dictionary as a list
boy = {'Name': 'Ali', 'Age': 21, 'Hieght': 6.7, 'Weight': 69, 'City':
'Peshawar', 'Religion': "Muslim"}

# Get the values of the dictionary


dict_values = boy.values()

print(dict_values)
dict_values(['Ali', 21, 6.7, 69, 'Peshawar', 'Muslim'])

9.Items
# The items() method returns the key-value pairs of the dictionary, as
tuples in a list
boy = {'Name': 'Ali', 'Age': 21, 'Hieght': 6.7, 'Weight': 69, 'City':
'Peshawar', 'Religion': "Muslim"}

# Get the key-value pairs of the dictionary


key_value_pairs = boy.items()

print(key_value_pairs)

dict_items([('Name', 'Ali'), ('Age', 21), ('Hieght', 6.7), ('Weight',


69), ('City', 'Peshawar'), ('Religion', 'Muslim')])

10. Fromkeys
In Python, the fromkeys() method is a method available on dictionary objects. It creates a new
dictionary with keys from a given iterable (such as a list, tuple, or set) and assigns the same value
to each key.

# The fromkeys() method returns a dictionary with the specified keys


from an iterable (such as a list, tuple, or set) and assigns the
specified value to each key
keys = ('Name1', 'Name2', 'Name3', 'Name4')
value = 'Ali'

keys_value = dict.fromkeys(keys, value)

print(keys_value)

{'Name1': 'Ali', 'Name2': 'Ali', 'Name3': 'Ali', 'Name4': 'Ali'}

11. Setdefault()
In Python, the setdefault() method is a built-in dictionary method used to access a key in a
dictionary. It checks if the key is present in the dictionary or not. If the key is present, it returns
the corresponding value. If the key is not present, it inserts the key into the dictionary with a
specified default value, and then returns that value.

# The setdefault() method returns the value of the item with the
specified key
boy = {'Name': 'Ali', 'Age': 21, 'Hieght': 6.7, 'Weight': 69, 'City':
'Peshawar', 'Religion': "Muslim"}

# Get the value with the specified key


value_with_key = boy.setdefault('Name', 'Yousaf')

print(value_with_key)

Ali

Nested Dictionaries
A dictionary can contain dictionaries, this is called nested dictionaries.

# Create a dictionary that contain three dictionaries


boys_details = {'boy1' : {'Name': 'Ali', 'Age': 23, 'Hieght': 6.7,
'Weight': 69, 'City': 'Peshawar', 'Religion': "Muslim"},
'boy2' : {'Name': 'Ahmad', 'Age': 21, 'Hieght': 6.5,
'Weight': 70, 'City': 'Peshawar', 'Religion': "Muslim"},
'boy3' : {'Name': 'Abas', 'Age': 22, 'Hieght': 6.4,
'Weight': 72, 'City': 'Peshawar', 'Religion': "Muslim"}}

print(boys_details)

{'boy1': {'Name': 'Ali', 'Age': 23, 'Hieght': 6.7, 'Weight': 69,


'City': 'Peshawar', 'Religion': 'Muslim'}, 'boy2': {'Name': 'Ahmad',
'Age': 21, 'Hieght': 6.5, 'Weight': 70, 'City': 'Peshawar',
'Religion': 'Muslim'}, 'boy3': {'Name': 'Abas', 'Age': 22, 'Hieght':
6.4, 'Weight': 72, 'City': 'Peshawar', 'Religion': 'Muslim'}}

# Create three dictionaries, then create one dictionary that will


contain the other three dictionaries
boy1 = {'Name': 'Ali', 'Age': 23, 'Hieght': 6.7, 'Weight': 69, 'City':
'Peshawar', 'Religion': "Muslim"},
boy2 = {'Name': 'Ahmad', 'Age': 21, 'Hieght': 6.5, 'Weight': 70,
'City': 'Peshawar', 'Religion': "Muslim"},
boy3 = {'Name': 'Abas', 'Age': 22, 'Hieght': 6.4, 'Weight': 72,
'City': 'Peshawar', 'Religion': "Muslim"}

# create a dictionary that contain the all three dictionaries


boys_detail = {
'boy1' : boy1,
'boy2' : boy2,
'boy3' : boy3,
}

print(boys_detail)

{'boy1': ({'Name': 'Ali', 'Age': 23, 'Hieght': 6.7, 'Weight': 69,


'City': 'Peshawar', 'Religion': 'Muslim'},), 'boy2': ({'Name':
'Ahmad', 'Age': 21, 'Hieght': 6.5, 'Weight': 70, 'City': 'Peshawar',
'Religion': 'Muslim'},), 'boy3': {'Name': 'Abas', 'Age': 22, 'Hieght':
6.4, 'Weight': 72, 'City': 'Peshawar', 'Religion': 'Muslim'}}

Access Items in Nested Dictionaries


# To access items from a nested dictionary, you use the name of the
dictionaries, starting with the outer dictionary
boys_details = {'boy1' : {'Name': 'Ali', 'Age': 23, 'Hieght': 6.7,
'Weight': 69, 'City': 'Peshawar', 'Religion': "Muslim"},
'boy2' : {'Name': 'Ahmad', 'Age': 21, 'Hieght': 6.5,
'Weight': 70, 'City': 'Peshawar', 'Religion': "Muslim"},
'boy3' : {'Name': 'Abas', 'Age': 22, 'Hieght': 6.4,
'Weight': 72, 'City': 'Peshawar', 'Religion': "Muslim"}}

# Acess the boy2 name


boy2_name = boys_details['boy2']['Name']
print(boy2_name)

Ahmad

You might also like