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

Filter function of python

filter function:-  Imagine we have a bunch of things, like a list of numbers, a collection of fruits, or any other group of items. Now, let's say we want to pick out only certain items from that group based on a specific condition. This is where the filter function comes in handy. Let's say we have a list of numbers, and we want to filter out only the even numbers. We define a condition-checking function that checks if a number is even. Then, we pass this function and the list of numbers to the filter function. It goes through each number, applies the condition-checking function, and keeps only the numbers that are even. Finally, it gives us a new list containing only the even numbers. def is_even(num):     return num % 2 == 0 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] filtered_numbers = list(filter(is_even, numbers)) print(filtered_numbers) Output [2, 4, 6, 8, 10] We can also utilize lambda expressions to define filtering conditions directly within the filter function. nu...

The Python Interpreter पायथन इंटरप्रेटर

The Python interpreter is a program that executes Python code. It reads and interprets Python scripts or interactive commands, converting them into machine-readable bytecode for the computer to execute.The interpreter is a crucial component for running Python code, facilitating both learning and development processes. Users can interact with the interpreter in two primary modes: पायथन इंटरप्रेटर एक प्रोग्राम है जो पायथन कोड को निष्पादित करता है। यह पायथन स्क्रिप्ट या इंटरैक्टिव कमांड को पढ़ता है और व्याख्या करता है, उन्हें कंप्यूटर द्वारा निष्पादित करने के लिए मशीन-पठनीय बाइटकोड में परिवर्तित करता है। दुभाषिया पायथन कोड चलाने के लिए एक महत्वपूर्ण घटक है, जो सीखने और विकास दोनों प्रक्रियाओं को सुविधाजनक बनाता है। उपयोगकर्ता इंटरप्रेटर के साथ दो प्राथमिक मोड में बातचीत कर सकते हैं:- 1. Interactive Mode:- In this mode we can launch the Python interpreter in our terminal or command prompt without specifying a script.This mode allows us to enter Python commands and see immediate results...

Flow of execution of a python program पाइथन प्रोग्राम के निष्पादन का प्रवाह

Flow of execution represents a general outline of Python program execution. The specific flow can vary based on the program's structure, control flow statements (if, else, for, while), and function calls. निष्पादन का प्रवाह पायथन प्रोग्राम निष्पादन की एक सामान्य रूपरेखा का प्रतिनिधित्व करता है। विशिष्ट प्रवाह प्रोग्राम की संरचना, नियंत्रण प्रवाह विवरण (यदि, अन्यथा, के लिए, जबकि), और फ़ंक्शन कॉल के आधार पर भिन्न हो सकता है। 1.) Start: The program begins execution. 1.) प्रारंभ: प्रोग्राम का निष्पादन प्रारंभ होता है। 2.) Import Modules: If necessary, the program imports external modules or libraries. 2.) मॉड्यूल आयात करें: यदि आवश्यक हो, तो प्रोग्राम बाहरी मॉड्यूल या लाइब्रेरी आयात करता है। 3.) Define Functions: Any functions used in the program are defined. 3.) फंक्शन्स को परिभाषित करें: प्रोग्राम में उपयोग किए गए किसी भी फंक्शन को परिभाषित किया जाता है। 4.) Main Program: The main body of the program starts. 4.) मुख्य प्रोग्राम: प्रोग्राम का मुख्य भाग प्रारंभ होता है। 5.) Initializat...