Skip to main content

Dictionary in Python पायथन में डिक्शनरी, Dictionary Operations डिक्शनरी ऑपरेशन , Dictionary functions and methods डिक्शनरी फंक्शन और विधियाँ

In Python, a dictionary is a collection of key-value pairs. Dictionaries are useful for storing and managing data that is organized by a unique identifier or "key," such as names and values associated with them. Dictionaries in Python are mutable, efficient, and suitable for a variety of operations, especially for managing data with unique keys.
पायथन में, एक शब्दकोश (डिक्शनरी) कुंजी-मूल्य युग्मों का एक संग्रह है। शब्दकोश डेटा को संग्रहीत करने और प्रबंधित करने के लिए उपयोगी होते हैं जो एक अद्वितीय पहचानकर्ता या "कुंजी" द्वारा व्यवस्थित होते हैं, जैसे कि उनके साथ जुड़े नाम और मान। पायथन में शब्दकोश परिवर्तनशील, कुशल और विभिन्न प्रकार के संचालन के लिए उपयुक्त हैं, विशेष रूप से अद्वितीय कुंजियों वाले डेटा को प्रबंधित करने के लिए।

Syntax:-

Creating a Dictionary शब्दकोश तैयार करना:- We can create a dictionary using curly braces {}
 हम मंझला कोष्टक {} का उपयोग करके शब्दकोश बना सकते हैं

my_dict = { "name": "Alice", "age": 25, "city": "New York" }

Dictionary Operations डिक्शनरी ऑपरेशन:-

1. Accessing Elements तत्वों तक पहुँचना:- We can access elements by their key.
हम तत्वों तक उनकी कुंजी द्वारा पहुँच सकते हैं।

name = my_dict["name"]                      # Output: Alice

2. Adding Elements तत्व जोड़ना:- We can add a new key-value pair. 
हम एक नया कुंजी-मूल्य युग्म जोड़ सकते हैं।

my_dict["city"] = "Los Angeles"         # Adds new key "city"

3. Updating Elements तत्वों को अद्यतन करना:- We can update an existing key.
हम किसी मौजूदा कुंजी को अद्यतन कर सकते हैं।

my_dict["age"] = 26                            # Updates the value of "age"

4. Removing Elements तत्वों को हटाना:- We can remove elements with pop(), del, or popitem() methods.
हम pop(), del, या popitem() विधियों से तत्वों को हटा सकते हैं।

my_dict.pop("name")                          # Removes "name" and returns its value
del my_dict["age"]                              # Removes "age"
last_item = my_dict.popitem()           # Removes and returns the last inserted item

5. Merging Dictionaries शब्दकोशों का विलय:- Combine two dictionaries using update() method.
update() विधि का उपयोग करके दो शब्दकोशों को संयोजित करें।

my_dict.update({"country": "USA", "city": "New York"})

Alternatively, We can use {*dict1, **dict2} or {*my_dict, "new_key": new_value} 
वैकल्पिक रूप से, हम {*dict1, **dict2} या {*my_dict, "new_key": new_value} का उपयोग कर सकते हैं।

6. Checking if Key Exists जाँच करें कि कुंजी उपस्थित है या नहीं:- Check if a key is in the dictionary.
जाँच करें कि कुंजी शब्दकोष में है या नहीं।

if "name" in my_dict:
  print("Name exists in dictionary")

7. Looping Through Keys and Values कुंजियों और मानों के माध्यम से पुनरावृति:- 
We can iterate over keys, values, or both.
हम कुंजियों, मानों या दोनों पर पुनरावृति कर सकते हैं।

for key in my_dict.keys():
   print(key)

for value in my_dict.values():
   print(value)

for key, value in my_dict.items():
   print(key, value)

8. Dictionary Comprehensions शब्दकोश समझ:- We can create dictionaries using comprehensions.
हम कॉम्प्रिहेंशन का उपयोग करके शब्दकोश बना सकते हैं।

squared_numbers = {x: x**2 for x in range(1, 6)} 
#Output:- {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

Dictionary functions and methods डिक्शनरी फंक्शन और विधियाँ:-

1. dict():- Creates an empty dictionary or converts a list of key-value pairs to a dictionary.
यह फंक्शन एक खाली शब्दकोश बनाता है या कुंजी-मूल्य जोड़ों की सूची को शब्दकोश में परिवर्तित करता है।

my_dict = dict()  # Empty dictionary
my_dict2 = dict([("name", "Alice"), ("age", 25)])
my_dict3 = dict(city="New York", country="USA")

2. get(key[, default]):- Returns the value for the specified key; if the key doesn’t exist, it returns None or the specified default value.
यह फंक्शन निर्दिष्ट कुंजी के लिए मान लौटाता है; यदि कुंजी उपलब्ध नहीं है, तो यह None या निर्दिष्ट डिफ़ॉल्ट मान लौटाता है।

my_dict.get("name")                                # Output: "Alice"
my_dict.get("country", "Not Found")      # Output: "Not Found"

3. pop(key[, default]):- Removes and returns the value of the specified key. If the key doesn’t exist, it returns the default value.
यह फंक्शन निर्दिष्ट कुंजी का मान निकालता है और लौटाता है। यदि कुंजी उपलब्ध नहीं है, तो यह डिफ़ॉल्ट मान लौटाता है।

my_dict.pop("age")        # Removes and returns the value for "age"

4. popitem():- Removes and returns the last inserted key-value pair as a tuple. Raises KeyError if the dictionary is empty.
यह फंक्शन अंतिम सम्मिलित कुंजी-मान युग्म को निकालता है तथा उसे टपल के रूप में लौटाता है। यदि शब्दकोश रिक्त है तो KeyError उठाता है।

my_dict.popitem()      # Removes and returns the last inserted key-value pair

5. clear():- Removes all elements from the dictionary, leaving it empty.
यह फंक्शन शब्दकोश से सभी तत्वों को हटा देता है, और उसे रिक्त छोड़ देता है।

my_dict.clear()

6. del my_dict[key]:- Deletes the specified key from the dictionary.
यह फंक्शन शब्दकोश से निर्दिष्ट कुंजी को हटाता है.

del my_dict["name"]

7. update(other_dict):- Adds key-value pairs from another dictionary or iterable of pairs to the dictionary. Existing keys are updated.
यह फंक्शन किसी अन्य शब्दकोश या युग्मों के पुनरावृत्त से कुंजी-मान युग्मों को शब्दकोश में जोड़ता है। विद्यमान कुंजियाँ अद्यतन की जा सकती हैं।

my_dict.update({"country": "USA", "city": "New York"})

8. keys():- Returns a view object with all keys in the dictionary.
यह फंक्शन शब्दकोश में सभी कुंजियों के साथ एक दृश्य ऑब्जेक्ट लौटाता है।

keys_view = my_dict.keys()     # dict_keys(['name', 'city'])

9. values():- Returns a view object with all values in the dictionary.
यह फंक्शन शब्दकोश में सभी मानों के साथ एक दृश्य ऑब्जेक्ट लौटाता है।

values_view = my_dict.values()   # dict_values(['Alice', 'New York'])

10. items():- Returns a view object with all key-value pairs as tuples.
यह फंक्शन सभी कुंजी-मान युग्मों को टपल के रूप में लेकर एक दृश्य ऑब्जेक्ट लौटाता है।

items_view = my_dict.items()   # dict_items([('name', 'Alice'), ('city', 'New York')])

11. copy():- Creates a shallow copy of the dictionary.
यह फंक्शन शब्दकोश की एक शाल्लो प्रतिलिपि बनाता है

new_dict = my_dict.copy()

12. setdefault(key[, default]):- Returns the value for the specified key. If the key is not in the dictionary, it inserts the key with the specified default value.
यह फंक्शन निर्दिष्ट कुंजी के लिए मान लौटाता है। यदि कुंजी शब्दकोश में नहीं है, तो यह निर्दिष्ट डिफ़ॉल्ट मान के साथ कुंजी सम्मिलित करता है।

my_dict.setdefault("age", 30)     # Adds "age" with value 30 if it doesn't exist 

Comments

Popular posts from this blog

Python IDLE पायथन आईडीएलई (इंटीग्रेटेड डेवलपमेंट एंड लर्निंग एनवायरनमेंट)

IDLE (Integrated Development and Learning Environment) is a graphical user interface (GUI) for the Python programming language. It includes the Python standard library and provides a convenient environment for writing, running, and debugging Python code. IDLE (इंटीग्रेटेड डेवलपमेंट एंड लर्निंग एनवायरनमेंट) पायथन प्रोग्रामिंग भाषा के लिए एक ग्राफिकल यूजर इंटरफेस (GUI) है। इसमें पायथन मानक लाइब्रेरी शामिल है और पायथन कोड लिखने, चलाने और डीबग करने के लिए एक सुविधाजनक वातावरण प्रदान करता है।  Key features of IDLE are:- आईडीएलई की मुख्य विशेषताएं हैं:-  1. Interactive Shell:- IDLE provides an interactive Python shell, allowing you to execute Python commands and see the immediate results. 1. इंटरएक्टिव शेल:- आईडीएलई एक इंटरैक्टिव पायथन शेल प्रदान करता है, जो आपको पायथन कमांड निष्पादित करने और तत्काल परिणाम देखने की अनुमति देता है।  2. Script Editor:- It includes a text editor for writing Python scripts. You can create, edit, and save Python files with a ".py" extension. 2. स्क्...

Operator's in Python पायथन में ऑपरेटर्स

Python supports various types of operators for performing operations on variables and values. Here are some common types of operators in Python: पायथन वेरिएबल्स और मानों पर संचालन करने के लिए विभिन्न प्रकार के ऑपरेटरों का समर्थन करता है। यहां पायथन में कुछ सामान्य प्रकार के ऑपरेटर हैं: 1. Arithmetic Operators:    + # Addition     - # Subtraction    * # Multiplication    / # Division    % # Modulus (remainder)    ** # Exponentiation    // # Floor Division 2. Comparison Operators:        == # Equal to    != # Not equal to    < # Less than    > # Greater than    <= # Less than or equal to    >= # Greater than or equal to     3. Logical Operators:       and # Logical AND    or # Logical OR    not # Logical NOT    4. Assignment Operators:    ...

Programming with Python Language Tutorial available in hindi and english पाइथन प्रोग्रामिंग टुटोरिअल हिंदी एवं अंग्रेजी भाषा में

 Unit -01 1. Introduction to python (पाइथन भाषा का परिचय) 2. History of Python Programming Language पाइथन प्रोग्रामिंग लैंग्वेज का इतिहास 3. Python Programming Language’s Features and Advantages पायथन प्रोग्रामिंग लैंग्वेज की प्रमुख विशेषताएं 4. Python’s Programming Language's Popularity पाइथन प्रोग्रामिंग लैंग्वेज की लोकप्रियता 5.The Future of Python Programming Language पायथन प्रोग्रामिंग लैंग्वेज का भविष्य 6. The Python Interpreter पायथन इंटरप्रेटर 7. The Python IDLE पाइथन आईडीएलई 8. Installation process of Python IDLE पायथन आईडीएलई की इंस्टालेशन प्रक्रिया 9. Dynamically typed and strongly typed features of python गतिशील रूप से टाइप और दृढ़ता से टाइप की गई भाषा पायथन 10. Basic Data Types of Python पायथन के बेसिक डेटा टाइप 11. Variables in python पायथन में वेरिएबल 12. Expressions in Python पायथन में अभिव्यक्तियां 13. Statements in Python पायथन में कथन 14. Operator's in Python पायथन में ऑपरेटर्स 15. Flow of execution of a python program पाइथन प्रोग्राम के निष्पादन का प्रवाह 16...