Today we will do more questions related to dictionaries for a thorough revision. In the process, we also will learn a few methods related to dictionaries.
Summary of the week-
- Day 18 We learnt about dictionaries. We checked out one example which covered many inbuilt functions related to dictionaries.
- Day 19 We practiced a few questions on dictionaries and had quite a thorough practice of dictionaries in Python. We did programs to fuse two separate lists to a single dictionary, paired out even numbers and made fancy dictionaries using for loops.
- Day 20 We learnt about hashtables and how to create them using dictionaries. We tried out simple and chained hashtables and worked out information retrieval using them.
Sample question-Concatenate two dictionaries.
We will now fuse two dictionaries to create a new one using the update()
method
a={1:10, 2:20}
b={3:30, 4:40}
c={}
for i in (a,b):
#for every element in dictionary a AND dictionary b,
c.update(i)
#update the dictionary to include these values.
print(c)
output-
{1: 10, 2: 20, 3: 30, 4: 40}
Conflict resolution- In case both the dictionaries have a same key, in that case the last value is held true by the update()
method.
a={1:10, 2:20}
b={3:30, 4:40 , 2:60}
c={}
d={}
for i in (a,b):
c.update(i)
print(c)
for i in (b,a):
d.update(i)
print(d)
{1: 10, 2: 60, 3: 30, 4: 40}
{1: 10, 2: 20, 3: 30, 4: 40}
Editing keys.
I know what you all must be thinking. Keys are non-mutable right?
Yes. they are. but here is a cleaver trick
a = {
"a":1,
"b":2,
"c":3,
"e":4
}
# Replace 'e' by 'd'
a["d"] = a.pop("e")
print(a)
{'b': 2, 'a': 1, 'c': 3, 'd': 4}
Today there are no exercises from my side..π...But wait!π I am providing a reference link for taking on a dictionary quiz to clear all your concepts!π
Dictionary quiz by PYnative
The quiz contains 14 Questions. 14/14 is the target
Comment your progress below by pasting screenshot of your scores!π€
βοΈ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. π
Please do follow me on Github and Star the Learning Python Repository which contains all the material for this courseπ
Thank you allπ
For those who have not yet made account in Dev.to, you can have a free easy sign-up using your mail or GitHub accounts. I would suggest the budding developers to create your GitHub free account right away. You would require to register sooner or later anyways
π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯
Next day will begin from Tuesdayπ
, Monday is reserved for.... MATLAB MONDAYSπ₯ Follow me for updates...