Python                                                          Home : www.sharetechnote.com

 

 

 

 

Python - Dictionary

 

Like a Set or List in Python, a Dictionary is a of distict elements. The main difference between a Dictionary and List/Array is that it assigns a specific key for every elements in it. Every elements in a dictionary is specified as {'Key' : Element }.

 

NOTE 1 : All the examples in this page are written in Python 3.x. It may not work if you use Pyton 2.x

NOTE 2 : All the examples in this page are assumed to be written/run on Windows 7 unless specifically mentioned. You MAY (or may not) need to modify the syntax a little bit if you are running on other operating system.

  • Creating a Dictionary: Direct Creation with Numeric Key - Example 1
  • Creating a Dictionary: Direct Creation with String Key - Example 2
  • Accessing a Dictionary : Using a Numeric Key - Example 3
  • Accessing a Dictionary : Using a String Key - Example 4
  • Accessing a Dictionary : Looping through every elements - Example 5
  • Replacing an Element - Example 6
  • Adding an Element : using update() - Example 7
  • Replacing / Adding in single step : update() - Example 8
  • Removing an element - Example 9  

 

 

Examples :

 

< Example 1 >

 

fruits = {1:"Apple",2:"BlueBerry",3:"Banana",4:"SrawBerry"}

print(fruits)

 

Result :----------------------------------

 

{1: 'Apple', 2: 'BlueBerry', 3: 'Banana', 4: 'SrawBerry'}

 

 

 

< Example 2 >

 

Food = {'Fruit':'Apple','Grain':'Rice','Vegetable':'Cabbage'}

print(Food)

 

Result :----------------------------------

 

{'Fruit': 'Apple', 'Grain': 'Rice', 'Vegetable': 'Cabbage'}

 

 

< Example 3 >

 

fruits = {100:"Apple",200:"BlueBerry",300:"Banana",400:"SrawBerry"}

print(fruits[200])

 

Result :----------------------------------

 

BlueBerry

 

 

< Example 4 >

 

Food = {'Fruit':'Apple','Grain':'Rice','Vegetable':'Cabbage'}

print(Food['Vegetable'])

 

Result :----------------------------------

 

Cabbage

 

NOTE : Try print(Food['Cabbage']) and see what you get. It will give you an error since 'Cabbage' is not a Key. it is an element.

 

 

< Example 5 >

 

Food = {'Fruit':'Apple','Grain':'Rice','Vegetable':'Cabbage'}

 

print('================')

for f in Food :

    print(f)

 

print('================')

foods = list(Food.items())

for F in foods :

    print(F)

 

print('================')

k = list(Food.keys())

 

for i in range(0,len(k)) :

    print(Food[k[i]])

 

print('================')

k = list(Food.keys())

 

for ki in k :

    print(ki,',',Food[ki])

 

Result :----------------------------------

 

================   <-- for f in Food :

Fruit

Grain

Vegetable

================   <-- for F in foods :

('Fruit', 'Apple')

('Grain', 'Rice')

('Vegetable', 'Cabbage')

================   <-- for i in range(0,len(k)) :

Apple

Rice

Cabbage

================   <-- for ki in k :

Fruit , Apple

Grain , Rice

Vegetable , Cabbage

 

 

 

< Example 6 >

 

Food = {'Fruit':'Apple','Grain':'Rice','Vegetable':'Cabbage'}

 

print('================')

foods = list(Food.items())

for f in foods :

    print(f)

 

Food['Fruit'] = 'Grape'

 

print('================')

foods = list(Food.items())

for f in foods :

    print(f)

 

 

Result :----------------------------------

 

================  <-- Original Dictionary

('Fruit', 'Apple')

('Grain', 'Rice')

('Vegetable', 'Cabbage')

================  <-- Dictionary After Change

('Fruit', 'Grape')

('Grain', 'Rice')

('Vegetable', 'Cabbage')

 

 

< Example 7 >

 

Food = {'Fruit':'Apple','Grain':'Rice','Vegetable':'Cabbage'}

 

print('================')

foods = list(Food.items())

for f in foods :

    print(f)

 

Food.update({'Noodle':'Pasta'})

 

print('================')

foods = list(Food.items())

for f in foods :

    print(f)

 

Result :----------------------------------

 

================    <-- Original Dictionary

('Fruit', 'Apple')

('Grain', 'Rice')

('Vegetable', 'Cabbage')

================    <-- Dictionary After Change

('Fruit', 'Apple')

('Grain', 'Rice')

('Vegetable', 'Cabbage')

('Noodle', 'Pasta')

 

 

< Example 8 >

 

Food = {'Fruit':'Apple','Grain':'Rice','Vegetable':'Cabbage'}

 

print('================')

foods = list(Food.items())

for f in foods :

    print(f)

 

Food.update({'Fruit':'Grape','Noodle':'Pasta'})

 

print('================')

foods = list(Food.items())

for f in foods :

    print(f)

 

Result :----------------------------------

 

================      <-- Original Dictionary

('Fruit', 'Apple')

('Grain', 'Rice')

('Vegetable', 'Cabbage')

================      <-- Dictionary After Change

('Fruit', 'Grape')    // This is replaced

('Grain', 'Rice')

('Vegetable', 'Cabbage')

('Noodle', 'Pasta')   // This is added

 

 

 

< Example 9 >

 

Food = {'Fruit':'Apple','Grain':'Rice','Vegetable':'Cabbage'}

 

print('================')

foods = list(Food.items())

for f in foods :

    print(f)

 

del Food['Fruit']

 

print('================')

foods = list(Food.items())

for f in foods :

    print(f)

 

 

Result :----------------------------------

 

================        <-- Original Dictionary

('Fruit', 'Apple')

('Grain', 'Rice')

('Vegetable', 'Cabbage')

================        <-- Dictionary After removing an element

('Grain', 'Rice')

('Vegetable', 'Cabbage')

 

 

 

< Example 10 >