Post

Weird Dictionaries simplified.

Weird Dictionaries simplified πŸ“š

visitors

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.

cat emoji gif

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..

This post is licensed under CC BY 4.0 by the author.

Trending Tags