Python – Dictionaries

After reading this Python Dictionaries topic, you will understand how to create the dictionary, and you will know how to accessing Items, changing Items and use Python dictionary built-in methods.

A Dictionary is a variable that collects same or different type of elements(items) does not exist in a sequence. The elements of the dictionary cannot repeat and dictionary elements support key. Unlike an index which supports integer only, a key can support integer or string.

How to create Dictionary in python?

The dictionary items should be written within the round brackets (i.e. ()) and each item separated by a comma (,).

Example:

Program (1): To demonstrate how to create and display dictionary in Python.

# storing sports items cricket, football and hockey in the dictionary
x={1:'cricket', 2:'football', 3:'hockey'}
# print a dictionary
print(x)

Output(1)

{1: 'cricket', 2: 'football', 3: 'hockey'}

Accessing Items from Dictionary

Example:

Program (1): To demonstrate how to create dictionary and access dictionary items in Python.

# storing sports items cricket, football and hockey in the dictionary
x={1:'cricket', 2:'football', 3:'hockey'}
print(x[1])
print(x[2])

Output(1)

cricket
football

Explanation: for Program(1) and Output(1)

  • statement print(x[1]) gives output cricket describe the first item of dictionary, associated with key integer value 1.
  • statement print(x[2]) gives output 3 describe the second item of dictionary, associated with key integer value 2.

Changing Items from Dictionary

Example:

Program (1): To show how changing dictionary items in Python.

# storing sports items cricket, football and hockey in the dictionary
x={1:'cricket', 2:'football', 3:'hockey'}
# print a dictionary
print(x)
#change cricket as item to basketball
x[1]= 'basketball'
# print dictionary with changed items
print(x)

Output(1)

{1: 'cricket', 2: 'football', 3: 'hockey'}
{1: 'basketball', 2: 'football', 3: 'hockey'}

Adding item to the Dictionary:

We can insert the item at the particular position defined by new key in the dictionary.

Example:

Program (1): To show how to add an item at the particular position in the dictionary.

# storing sports items cricket, football and hockey in the dictionary
x={1:'cricket', 2:'football', 3:'hockey'}
# insert item into the dictionary.
x[4]='basketball'
# print dictionary
print(x)

Output(1)

{1: 'cricket', 2: 'football', 3: 'hockey', 4: 'basketball'}

Using loop over the Dictionary

Using a loop in the dictionary means we move through the dictionary and visit each item of the dictionary.

Example:

Program (1): To show how to use a loop on the dictionary to access dictionary items in Python.

# storing sports items cricket, football and hockey in the dictionary
x={1:'cricket', 2:'football', 3:'hockey'}
# print a dictionary
print(x)
# use for loop in order to print each item of the dictionary
for i in x:
print(x[i])

Output(1)

{1: 'cricket', 2: 'football', 3: 'hockey'}
cricket
football
hockey

Dictionary handling built-in methods

Some of commonly used dictionary handling methods like len(), clear(), pop() and clear().

len() : This method gives the total number of items in the dictionary.

# storing sports items cricket, football and hockey in the dictionary
x={1:'cricket', 2:'football', 3:'hockey'}
# using len() method to calculate to the total number of items in the dictionary.
print(len(x))

Output(1)

3

del() : This method remove an item from the dictionary.

# storing sports items cricket, football and hockey in the dictionary
x={1:'cricket', 2:'football', 3:'hockey'}
# using del() method to remove cricket as item from the dictionary
del(x[1])
# print dictionary
print(x)

Output(1)

{2: 'football', 3: 'hockey'}

pop() : This method also removes an item from the particular position defined by a key of the dictionary.

# storing sports items cricket, football and hockey in the dictionary
x={1:'cricket', 2:'football', 3:'hockey'}
# using pop() item from the dictionary.
x.pop (2)
# print dictionary
print(x)

Output(1)

{1: 'cricket', 3: 'hockey'}

clear() : This method remove all items from the dictionary.

# storing sports items cricket, football and hockey in the dictionary
x={1:'cricket', 2:'football', 3:'hockey'}
# using clear() method to remove all items
x.clear()
# print dictionary
print(x)

Output(1)

{}

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *