Python Dictionary:- Python Programming

0
132
Python Dictionary- Python Programming
Python Dictionary- Python Programming

Dictionary is a useful container in Python. The items in the dictionary are in pairs. One is called Key, the other is called Value.

Remember the friend list? We want to store his age with the friend’s name. For him:

friends = {'Shuvo':15, 'Tuhin':13, 'Tanvir':14, 'Dipu':13, 'Siyam':14}

Above we have created a dictionary. In which what is the name of the friends and the value is their age. We can find out the type of the container above

friends = {'Shubo':15, 'Tuhin':13, 'Tanvir':14, 'Dipu':13, 'Siyam':14}
print ( type(friends) ) 

Which will give us the output <type ‘dict’>

We can specify keyword arguments like this:

friends = dict(Shuvo = 15, Tuhin = 13, Tanvir = 14, Dipu = 13, Siyam = 14) 

To access dictionary values:

friends = dict(Shuvo = 15, Tuhin = 13, Tanvir = 14, Dipu = 13, Siyam = 14)
print (friends) 

Now if we want to find the value of a key, for that:

friends = {'Shuvo':15, 'Tuhin':13, 'Tanvir':14, 'Dipu' :13 , 'Siyam':14 }
print (friends['Shuvo'])

What to print all:

 friends = {'Shuvo':15, 'Tuhin':13, 'Tanvir':14, 'Dipu' :13 , 'Siyam':14 }
print (list(friends))

Iteration / loop:

We can easily run iteration on dictionary items

friends = {'Shuvo':15, 'Tuhin':13, 'Tanvir':14, 'Dipu' :13 , 'Siyam':14 }
for key in friends:
print ('Age of ', key, 'is: ', friends[key])

Or in this way

friends = { 'Shuvo ':15, 'Tuhin':13, 'Tanvir':14, 'Dipu' :13 , 'Siyam':14 }
for k,v in friends .items():
print ('Age of ', k, ' is:', v)

To add new items:

friends = {'Shuvo':15, 'Tuhin':13, 'Tanvir':14, 'Dipu' :13 , 'Siyam':14 }
friends ['Mahir'] = 16
print (friends)

To update an item:

If your friends are getting older, they need to be updated, aren’t they?

friends = {'Shuvo':15, 'Tuhin':13, 'Tanvir':14, 'Dipu' :13 , 'Siyam':14 }
friends ['Tuhin'] = 14
print (friends)

Updated Tuhin’s age here.

To delete an item from the dictionary:

friends = {'Shuvo':15, 'Tuhin':13, 'Tanvir':14, 'Dipu' :13 , 'Siyam':14 }
print(friends)
del friends[' Shuvo']
print(friends)

To delete all items:

friends = {'Shuvo':15, 'Tuhin':13, 'Tanvir':14, 'Dipu' :13 , 'Siyam':14 }
print(friends)
friends.clear();
print(friends) 

Now print and see if there are any items.

If we want, we can delete the entire dictionary in this way

friends = {'Shuvo':15, 'Tuhin':13, 'Tanvir':14, 'Dipu':13, 'Siyam':14}
del friends 

len ()

len () can find a dictionary length. E.g.

friends = { 'Shuvo ':15, 'Tuhin':13, 'Tanvir':14, 'Dipu' :13 , 'Siyam':14 }
print(len(friends)) 

Which will print 5.

In addition to these, Python has a built-in Sets data structure. Which is a lot like a list. We can place the same item more than once in the list. And keep all the unique items in the set. Besides, we can create our own data structures if we want. Can create a linked list, stack, queue, etc. Knowing about these, you can try to implement it yourself using the list.

Note: More Info (Tech24InfoBD) (Python Dictionary)

LEAVE A REPLY

Please enter your comment!
Please enter your name here