Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 21, 2021 04:13 am GMT

Learning Python-Basic course: Day 19, Practicing Dictionary exercises

Today we are going to solve some questions related to dictionaries. So in case you have missed yesterday's blog where we have covered dictionaries in depth, click here.

Just a bit of revision of dictionaries which we covered yesterday.
Defination-

A dictionary is defined as a general-purpose data structure for storing a group of objects. A dictionary is associated with a set of keys and each key has a single associated value.

Dictionaries are used for retrieving a value from a key
In dictionaries, the keys are mapped with specific values. They are somewhat similer to hashtables. we will cover hashtables in tomorrow's part, but today we will first take a closer look at how dictionaries operate by solving some questions related to them.

Sample program 1
Let's see how to fuse two separate lists into a key-value pair dictionary.

a=["a","b","c","d"]#list -keyb=(1,2,3,4)#tuple- value#Method 1c={} #empty dictionaryfor i in range(0,len(a)):    c[i]=a[i]print(c)#Method 2d=dict(zip(a,b))print(d)

In the above program, we have fused the keys(list a) and the values(tuple b) into a dictionary(c and d ). In the second method, we have used two methods dict() and zip(). The dict() method is used to create a dictionary while the zip method is used to zip together two key-value pairs.

Sample question 2)
Write code to make this dictionary using for loop-

{0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}

Solution-

a={}for i in range(0,10):    a[i]=2* i print(a)

Exercise 1) Modify the above code to give the following output-

{'d': 16, 'c': 9, 'a': 1, 'b': 4}

(Don't mind the order)

Sample question 3) Write a program for making the dictionary shown below using for loops-

a={    1: [1, 2, 3, 4, 5],    2: [2, 3, 4, 5, 6],     3: [3, 4, 5, 6, 7],    4: [4, 5, 6, 7, 8],    5: [5, 6, 7, 8, 9]}

Solution-

a={}for i in range(1,6):    a[i]=[]    for j in range(i,i+5):        a[i].append(j)print(a)

OUTPUT-

{1: [1, 2, 3, 4, 5], 2: [2, 3, 4, 5, 6], 3: [3, 4, 5, 6, 7], 4: [4, 5, 6, 7, 8], 5: [5, 6, 7, 8, 9]}

Exercise 2)- Modify the above program for making the following dictionary-

b={    1:[1],    2:[1,2],    3:[1,2,3],    4:[1,2,3,4],    5:[1,2,3,4,5]}

Answers to the above exercise will be found as usual in the Learning-Python Repository

Fun exercise

Can you find the levels of nesting? first one is solved for you. Answer in the comments below!

a={[([1,2,3])]:[[{1:2}]]}#key: List in a tuple in a list value: dictionary in a list in a list.b={[1,2]:(((1,2,3)))}c={1:[([(1,2,3)])]d={[({[1,2]:[(1,2,3)]})]:[((1,2,{2:3}))]}#invalid... Can you find out why?e=[[(a,b),c]:(d,(a))]

So friends that's all for now. Hope you all are having fun. Please let me know in the comment section below . And don't forget to like the post if you did. I am open to any suggestions or doubts. Just post in the comments below or gmail me.
Thank you all

Follow me on GitHub for updates


Original Link: https://dev.to/aatmaj/learning-python-basic-course-day-19-practicing-dictionary-exercises-1723

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To