Weird Dictionaries simplified.
Weird Dictionaries simplified π
Hey everyone π
This is my first ever blog
on the internet, I am excited to share my journey with you.
So we are here for Dictionaries right?
Q: What are Dictionaries?
A: A book? A book that contains many pages? A book that has meanings of other words??
Well, technically yesβ¦ But Pythonically π no!!
So, what is a Dictionary in Python?
Lets compare it with real world dictionaries. Imagine a real-world dictionary that helps you find the meaning of words. Python dictionaries work similarly, but instead of words and meanings, they store key-value
pairs. These key-value pairs allow you to quickly find information based on a unique βkey.β
In simple words, think of a Dictionary as Locker Room π, where each key π opens a specific lock! π
So, what are we waiting for? Lets start with a simple example.
Creation
1
2
3
4
5
6
7
8
9
# Creating an empty dictionary
my_dict = {}
# Adding key-value pairs
my_dict['apple'] = 'A fruit that keeps the doctor away π'
my_dict['banana'] = 'Yellow fruit loved by monkeys π'
my_dict['cherry'] = 'Small red fruit used in desserts π'
print(my_dict)
Output:
1
{'apple': 'A fruit that keeps the doctor away π', 'banana': 'Yellow fruit loved by monkeys π', 'cherry': 'Small red fruit used in desserts π'}
Congratulations! π Youβve just created your own Python dictionary! The output will show all the key-value pairs stored in the dictionary.
Now wait!.. Hold your horses..π΄
Let me explain!
Explanation
1
my_dict = {}
- This line initializes an empty dictionary named
my_dict
. In Python, dictionaries are defined using curly braces {}.
1
2
3
my_dict['apple'] = 'A fruit that keeps the doctor away π'
my_dict['banana'] = 'Yellow fruit loved by monkeys π'
my_dict['cherry'] = 'Small red fruit used in desserts π'
- These lines add key-value pairs to the
my_dict
dictionary. - In Python dictionaries, each value is associated with a unique key. Here, βappleβ, βbananaβ, and βcherryβ are keys π, and the corresponding strings are their associated values βΉοΈ.
- To add a key-value pair to a dictionary, we use square brackets ([]) with the key π inside them on the left side of the assignment operator (=), followed by the value on the right side.
- Well, you know the
print
part, right ?
Accessing
Now comes the usage π§ͺ
1
print(my_dict['apple'])
- By specifying the key (βappleβ in this case), you can instantly retrieve the associated value (βA fruit that keeps the doctor away πβ).
Usage
WHY Dictionaries ? π€ π
- Fast Information Retrieval π: Dictionaries allow lightning-fast retrieval of values associated with a given key.
- Flexibility π§°: Dictionaries can be used to store any type of data, including lists, dictionaries, and other complex data structures.
- Dynamic Nature πΏ: Dictionaries can be modified easily by adding, updating, or removing key-value pairs.
- And many more! π:
Conclusion
Python dictionaries are not just another data structure; theyβre magical tools for organizing information effortlessly. These are helpful in complex algorithms and data structures.
Thanks very much! Happy hacking!
by @piyushduggal-source (mr_unchained)
Note: I did not use ChatGPT, kasam se..