Python Dictionaries
What is a Dictionary?
A dictionary in Python is an unordered collection of items. Each item is stored as a key-value pair. Dictionaries are written with curly braces {}
and keys and values are separated by a colon :
. Keys must be unique and immutable (like strings, numbers, or tuples), while values can be of any data type.
Python में डिक्शनरी एक unordered संग्रह है जिसमें items key-value जोड़े के रूप में संग्रहित होते हैं। डिक्शनरी को curly braces {}
में लिखा जाता है और key और value के बीच :
होता है। Keys unique और immutable (जैसे string, number या tuple) होनी चाहिए, जबकि values किसी भी डेटा प्रकार की हो सकती हैं।
Why Use Dictionaries?
Dictionaries are very useful because:
- Fast Lookup: Access values quickly using keys.
- Key-value Structure: Useful for representing real-world data like student records, where you have attributes and their values.
- Flexible Data Storage: Values can be complex data types like lists or even other dictionaries.
- Unordered but Indexed: Unlike lists, dictionaries are unordered but you access data by keys.
डिक्शनरी बहुत उपयोगी हैं क्योंकि:
- तेज़ खोज: Keys का उपयोग करके values तक जल्दी पहुँचें।
- Key-value संरचना: वास्तविक दुनिया के डेटा जैसे छात्र रिकॉर्ड को दर्शाने के लिए उपयुक्त, जहां attributes और उनकी values होती हैं।
- लचीला डेटा संग्रह: Values जटिल डेटा प्रकार जैसे lists या अन्य dictionaries भी हो सकती हैं।
- unordered लेकिन indexed: Lists के विपरीत, dictionaries unordered होती हैं लेकिन आप data को keys से एक्सेस करते हैं।
Where to Use Dictionaries?
Use dictionaries when you need to:
- Store related data as key-value pairs, e.g., a person's name and age.
- Implement data structures like hash tables or maps.
- Represent JSON-like data.
- Quickly lookup data without needing order.
डिक्शनरी का उपयोग तब करें जब आपको:
- संबंधित डेटा को key-value जोड़े के रूप में संग्रहित करना हो, जैसे किसी व्यक्ति का नाम और उम्र।
- hash tables या maps जैसे डेटा संरचनाएं बनानी हों।
- JSON जैसी डेटा संरचना का प्रतिनिधित्व करना हो।
- order की आवश्यकता नहीं है और तेज़ lookup चाहिए।
Python Dictionary Examples
Example 1: Creating a Simple Dictionary
my_dict = {'name': 'Rahul', 'age': 22, 'city': 'Delhi'}
print(my_dict)
Create a dictionary with keys 'name', 'age', and 'city'.
'name', 'age', और 'city' keys के साथ dictionary बनाएं।
Output:
Example 2: Accessing Values Using Keys
my_dict = {'name': 'Rahul', 'age': 22, 'city': 'Delhi'}
print(my_dict['name'])
Access the value for key 'name'.
'name' key का value access करें।
Output:
Example 3: Adding a New Key-Value Pair
my_dict = {'name': 'Rahul', 'age': 22}
my_dict['city'] = 'Delhi'
print(my_dict)
Add a new key 'city' with value 'Delhi'.
नई key 'city' और value 'Delhi' जोड़ें।
Output:
Example 4: Modifying an Existing Value
my_dict = {'name': 'Rahul', 'age': 22, 'city': 'Delhi'}
my_dict['age'] = 23
print(my_dict)
Change the value of key 'age' to 23.
'age' key का value 23 में बदलें।
Output:
Example 5: Removing a Key-Value Pair
my_dict = {'name': 'Rahul', 'age': 22, 'city': 'Delhi'}
my_dict.pop('city')
print(my_dict)
Remove the key 'city' and its value using pop().
pop() का उपयोग करके 'city' key और इसकी value हटाएं।
Output: