List Tuple Dictionary Python

List Tuple Dictionary Python

We can use list, tuple and set to represent a group of individual objects as a single entity. If we want represent a group of objects as key-value pairs then we go for dictionary.

हम एकल इकाई के रूप में अलग-अलग वस्तुओं के समूह का प्रतिनिधित्व करने के लिए लिस्ट, टपल और सेट का उपयोग कर सकते हैं। यदि हम की-वैल्यू जोड़े के रूप में ऑब्जेक्ट्स के एक समूह का प्रतिनिधित्व करना चाहते हैं तो हम डिक्शनरी के लिए जाते हैं।

Creating a dictionary is as simple as placing items inside curly braces f} separated by comma. An item has a key and the corresponding value expressed as a pair, key: value. While values can be of any data type and can repeat; keys must be of immutable type (string, number or tuple with immutable elements) and must be unique.

एक डिक्शनरी बनाना उतना ही सरल है जितना कि कर्ली ब्रेकेट{} के अंदर आइटम कॉमा द्वारा अलग किया जाना। एक आइटम में एक की और एक जोड़ी के रूप में व्यक्त की जाने वाली वैल्यू है, की:वैल्यू। हालांकि वैल्यू किसी भी डेटा प्रकार के हो सकते हैं और दोहरा सकते हैं, कीज अपरिवर्तनीय प्रकार (स्ट्रिंग, नम्बर या अपरिवर्तनीय एलीमेंट के सौंथ टपल) की होनी चाहिए और अद्वितीय होनी चाहिए।

The main properties of Python Dictionaries:

पायथन डिक्शनरी के मुख्य गुण

Duplicate keys are not allowed but values can be duplicated

डुप्लिकेट की एलाउड नहीं है लेकिन वैल्यू को डुप्लिकेट किया जा सकता है

Heterogeneous objects are allowed for both key and values

विषम वस्तुओं को की और वैल्यू दोनों के लिए अनुमति दी जाती है

Insertion order is not preserved इंसर्शन ऑर्डर संरक्षित नहीं है

Dictionaries are mutable डिक्शनरी परस्पर हैं

Dictionaries are dynamic डिक्शनरी डॉयनॉमिक हैं

Indexing and slicing concepts are not applicable

इंडेक्सिंग और स्लाइसिंग कॉन्सेप्ट लागू नहीं है

Creating dictionary-डिक्शनरी बनाना

We are creating empty dictionary. We can add entries as follows:

हम इम्पटी डिक्शनरी बना रहे हैं। हम निम्नानुसार प्रविष्टियाँ जोड़ सकते हैं:

# empty dictionary
dct = { }
# using dict() function
dct = dict()
dct [1] = ‘aaa’
dct [2] = ‘bbb’
dct [3] = ‘ccc’
print (dct)
dct[‘a’] = ‘python’
dct [‘b’] = ‘python ‘
dct[‘c’] = ‘python’
print (dct)

Output:
{1: ‘aaa’, 2: ‘bbb’, 3: ‘ccc’}
{1: ‘aaa’, 2: ‘bbb’, 3: ‘ccc’, ‘a’: ‘python’, ‘b’: ‘python ‘, ‘c’: ‘python’}

We can also create dictionary as follows…

हम निम्नानुसार डिक्शनरी भी बना सकते हैं….

d = (key:value, key:value
d = {‘ ro11_no’ : ‘101’, ‘name’ : ‘krishna’, ‘marks’ : 456}

To access data from dictionary
डिक्शनरी से डेटा का उपयोग करने के लिए

Since, dictionaries maps keys to values and these key-value pairs provide a useful way to store data in Python. It is used to hold data that are related, such as the information contained in an ID or a user profile. It is constructed with curly braces on either side {}.

चूंकि, डिक्शनरी वैल्यू के लिए एक की मैप करते हैं और ये की-वैल्यू जोड़े पायथन में डेटा स्टोर करने का एक उपयोगी तरीका प्रदान करते हैं। इसका उपयोग उन डेटा को रखने के लिए किया जाता है जो संबंधित हैं, जैसे कि एक आईडी या उपयोगकर्ता प्रोफाइल में निहित जानकारी। डिक्शनरी का निर्माण दोनों तरफ की ब्रेसेस के साथ किया जाता है।

Note: In Python, we can access data from dictionaries using keys.

पायथन में, हम कीज का उपयोग करके डिक्शनरी से डेटा तक पहुँच सकते हैं ।

For example:

d = { ‘roll_no’ : ‘101’, ‘name’ : ‘krishna’, ‘marks’ : 456}
print (d[‘roll_no’])
print (d[‘name’])
print (d[‘marks’])

Output:
101
krishna
456

If the specified key is not available, then we will get KeyError message

यदि निर्दिष्ट की उपलब्ध नहीं है, तो हमें की एरर मैसेज मिलेगा

Example:

d = {‘roll_no’ : ‘101’, ‘name’ : ‘krishna’, ‘marks’ :456}
print (d[‘roll_no’])
print (d[‘total’])

Output:
101 Traceback (most recent call last):
File “C:/Python3.8/HP/test.py”, line 5, in <module>
print (d[‘total’]). KeyError: ‘total’

We can prevent this by checking whether key is already available or not by using has_key () function or by using operator. But has_key () function is available only in Python 2 but not in Python 3. Hence compulsory we have to use ‘in’ operator.

हम यह देख कर रोक सकते हैं कि has_key() फंक्शन का उपयोग करके या ऑपरेटर का उपयोग करके की पहले से ही उपलब्ध है या नहीं। लेकिन has_key() फंक्शन केवल पायथन 2 में उपलब्ध है, लेकिन पायथन 3 में नहीं। इसलिए अनिवार्य है कि हमें ‘in’ ऑपरेटर का उपयोग करना होगा।

Example:

d = { 'roll_no' : '101', 'name' : 'krishna', 'marks':456} 
if 'total' in d:
    Sprint (d['total']) 
else:
    print ("Given key is not available...")

Output:
Given key is not available…

While indexing is used with other container types to access values, dictionary uses keys. Key can be used either inside square brackets or with the get() method.

जबकि इंडेक्सिंग का उपयोग अन्य कंटेनर टाईप्स के साथ वैल्यू तक पहुंचने के लिए किया जाता है, डिक्शनरी कीज का उपयोग करता है। की का उपयोग या तो स्क्वैयर ब्रैकेट के अंदर या get() मेथड से किया जा सकता है।

The difference while using get() is that it returns None instead of KeyError, if the key is not found. get() का उपयोग करते समय रिटर्न None आता है, तो यह की एरर है, यदि की नहीं मिली है।

Example:

dict = { ‘name’ : ‘Buddha’, ‘age’ : 28}
print (dict[‘name’])
print (dict.get(‘age’))
# Trying to access keys which doesn’t exist throws error dict.get(‘address’)
print(“dict.get (‘address’) does’t throws an error”)
dict[‘address’]

Output:
Buddha
28
dict.get(‘address’) does’t throws an error
Traceback (most recent call last):
File “C:\Users\HP\Desktop\1.py”, line 6, in <module>
    dict[‘address’]
KeyError: ‘address’

Updating elements in a dictionary
डिक्शनरी के एलिमेंट को अपडेट करना

If key is not available then a new entry will be added to the dictionary with the specified key-value pair, otherwise old value will be replaced with new value.

यदि की उपलब्ध नहीं है, तो निर्दिष्ट की-वैल्यू जोड़ी के साथ डिक्शनरी में एक नई प्रविष्टि जोड़ी जाएगी, अन्यथा पुराने वैल्यू को नए वैल्यू के साथ बदल दिया जाएगा।

d[key] = value

Example:

dct = {‘name’: ‘Gyancs’, ‘age’: 28}
print (“Before upgradation dictionary values”)
print (dct)
print(“reference id value is : “, id (dct))
dct[‘age’] = 39
dct[‘address’] = ‘Lucknow, UP’
print(“After upgradation dictionary values”)
print (dct)
print(“reference id value is : “, id (dct))

Output:
Before upgradation dictionary values
{‘name’: ‘Gyancs’, ‘age’: 28}
reference id value is :  51147712
After upgradation dictionary values
{‘name’: ‘Gyancs’, ‘age’: 39, ‘address’: ‘Lucknow, UP’}
reference id value is :  51147712

In this example, before and after reference id values are same, so that dictionaries are mutable.

इस उदाहरण में, रिफरेन्स आईडी के पहले और बाद के वर्जन समान हैं, ताकि डिक्शनरी परस्पर भिन्न हों।

Example: Write a program to enter name and percentage marks in a dictionary and display information on the screen.

एक डिक्शनरी में नाम और प्रतिशत अंक दर्ज करने और स्क्रीन पर जानकारी प्रदर्शित करने के लिए एक प्रोग्राम लिखें।

ns = int (input ("Enter number of students : "))
i = 1 
record = { } 
while i<=ns:
    name = input ("Enter name of the student : ") 
    pmarks = input ("Enter % marks of the student :") 
    record [name] = pmarks
    i = i + 1 
print ("Name of the student lt % of the student") 
for x in record :
    print("\t",x,"\t\t", record [x])

Output:
Enter number of students : 2
Énter name of the student : rishab
Enter % marks of the student : 95
Enter name of the student : pradeep
Enter % marks of the student : 97
Name of the student % of the student
rishab  95
pradeep 97

Iterating over a Dictionary
एक शब्दकोष से अधिक

No method is needed to iterate over the keys of a dictionary:

शब्दकोश की कीज पर पुनरावृति करने के लिए किसी विधि की आवश्यकता नहीं है:

d = {'a':100, 'b':200,'c':300,'d':400} 
for key in d:
    print (key) 

Output:
a
b
c
d

But it’s possible to use the method keys (), but we will get the same result:

लेकिन मेथड keys() का उपयोग करना संभव है, लेकिन हम एक ही परिणाम प्राप्त करेंगे:

d = {'a':100, 'b':200, 'c':300,'d':400} 
for key in d :
    print (key) 

Output:
The method values() is a convenient way for iterating directly over the values:

लेकिन मेथड keys()का उपयोग करना संभव है, लेकिन हम एक ही परिणाम प्राप्त करेंगे

d = {'a':100, 'b':200, 'c':300,'d':400}
for key in d.values():
    print (key)

Output:
100
200
300
400

The above loop is logically equivalent to the following one:

उपरोक्त लूप तार्किक रूप से निम्न के बराबर है:

for key in d:
print (d[key])

Note: While executing the program you must sure about the indenting of the code.

Deleting elements in a dictionary
डिक्शनरी में एलीमेंट को हटाना

In Python, We can remove a particular item in a dictionary in many ways.

पायथन में, हम किसी डिक्शनरी में किसी विशेष आइटम को कई तरीकों से निकाल सकते हैं।

pop(): This method removes as item with the provided key and returns the value.

यह मेथड प्रदान की गई की के साथ आइटम को हटा देती है और वैल्यू रिटर्न कर देती है।

popitem(): This method removes and return an arbitrary item (key, value) form the dictionary.

यह मेथड एक आर्बिट्रेरी आइटम (की, वैल्यू) को निकालती है और रिटर्न आती है।

cler(): This method removes all the items at once.

यह मेथड एक ही बार में सभी ऑइटम को हटा देती है।

del keyword: This keyword to remove individual items or the entire dictionary itself.

यह कीवर्ड व्यक्तिगत आईटम या संपूर्ण डिक्शनरी को हटाने के लिए है।

Example:

d = {1 : 100,2:200, 3 : 300, 4 : 400, 5:500 }
print (d)
print (“remove a particular item, returns value of the deleted item”)
print (d.pop (4))
print (d)
print (“remove an arbitrary item”)
print (d.popitem ())
print (d)
print (“delete a particular item”)
del d[1]
print (d)
print (‘remove all items’)
d.clear()
print(d)
print(‘delete the dictionary itself’)
del d
print (d)

Output:
{1: 100, 2: 200, 3: 300, 4: 400, 5: 500}
remove a particular item, returns value of the deleted item
400
{1: 100, 2: 200, 3: 300, 5: 500}
remove an arbitrary item
(5, 500)
{1: 100, 2: 200, 3: 300}
delete a particular item
{2: 200, 3: 300}
remove all items
{}
delete the dictionary itself
Traceback (most recent call last):
  File “C:\Users\HP\Desktop\1.py”, line 17, in <module>
    print (d)
NameError: name ‘d’ is not defined

Sorting the Dictionary
डिक्शनरी में शॉटिंग

In the dictionary, you can also sort the elements. For example, if we want to print the name of the elements of our dictionary alphabetically we have to use for loop’. It will sort each element of dictionary accordingly.

डिक्शनरी में, आप एलीमेंट को भी शॉर्ट कर सकते हैं। उदाहरण के लिए, यदि हम अपने डिक्शनरी के एलीमेंट के नाम को वर्णानुक्रम में छापना चाहते हैं तो हमें ‘for loop’ का उपयोग करना होगा। यह तदनुसार डिक्शनरी के प्रत्येक एलीमेंट को क्रमबद्ध करेगा।

Example:

Dict = { 'somu': 18, 'ramu':12, 'Isita':22, 'bheemu':25} 
Boys = { 'somu': 18, 'ramu' :12, 'bheemu':25} 
Girls = {'sita':22} 
Students = list (Dict. keys ()) 
Students.sort () 
for s in Students:
    print (" : " . join ( (s, str (Dict [s] ))))

Output:
Isita : 22
bheemu : 25
ramu : 12
somu : 18

We declared the variable students for our dictionary “Dict.”

हमने अपने डिक्शनरी “Dict.” के लिए वैरियेबल छात्रों की घोषणा की।

Then we use the code Students.sort, which will sort the element inside our dictionary

फिर हम Students.sort कोड का उपयोग करते हैं, जो हमारे डिक्शनरी के अंदर के एलीमेंट को क्रमबद्ध करेगा।

But to sort each element in dictionary, we run the for loop by declaring variable S

लेकिन डिक्शनरी में प्रत्येक एलीमेंट को शॉर्ट करने के लिए, हम वैरियेबल S घोषित करके लूप के लिए चलते हैं।

Now, when we execute the code the for loop will call each element from the dictionary, and it will print the string and value in an order

अब, जब हम कोड निष्पादित करते हैं तो लूप फॉर डिक्शनरी से प्रत्येक एलीमेंट को बुलाएगा, और यह एक क्रम में स्ट्रिंग और वैल्यू को प्रिंट करेगा।

setdefault()

In Python, if the key is already available then setdefault() function returns the corresponding value. If the key is not available then the specified key-value will be added as new item to the dictionary.

पायथन में, यदि की पहले से ही उपलब्ध है तो setdefault() फंक्शन संबंधित वैल्यू पर रिटर्न करता है। यदि की उपलब्ध नहीं है, तो निर्दिष्ट की-वैल्यू को डिक्शनरी में नए आइटम के रूप में जोड़ा जाएगा।

d.setdefault (key, value)

Example:

d = {1 : “rama”, 2 : “krishna”, 3 : “murthy” }
print (d.setdefault (4, “vinay”))
print (d)
print (d.setdefault (1, “sachin”) )
print (d)

Output:
vinay
{1: ‘rama’, 2: ‘krishna’, 3: ‘murthy’, 4: ‘vinay’}
rama
{1: ‘rama’, 2: ‘krishna’, 3: ‘murthy’, 4: ‘vinay’}

copy(): To create exactly duplicate dictionary(cloned copy). This method returns a shallow copy of the dictionary. It doesn’t modify the original dictionary.

बिल्कुल डुप्लिकेट डिक्शनरी (क्लोन कॉपी) बनाने के लिए। इस मेथड में डिक्शनरी की शैलो प्रति मिलती है। यह मूल डिक्शनरी को संशोधित नहीं करता है।

Syntax: dict.copy()

one = {1 : ‘one’, 2 : ‘two ‘ }
two = one.copy ()
print (‘Original: ‘, one, ‘id: ‘, id (one))
print ( ‘ New : ‘, two, ‘ id : ‘, id (two) )

Output:
Original:  {1: ‘one’, 2: ‘two ‘} id:  42693368
New :  {1: ‘one’, 2: ‘two ‘}  id :  42693048

len(): The len() function returns the number of items in an object. Failing to pass an argument or passing an invalid argument will raise a TypeError exception.

len() फंक्शन किसी ऑब्जेक्ट में आइटम की संख्या रिटर्न करता है। एक तर्क को पारित करने या एक अमान्य तर्क को पारित करने में विफल होने से एक टाईप एरर एक्सेप्शन बढ़ जाएगा।

Syntax: len(s)

dict= []
print (dict, ‘ length is ‘, len (dict))
dict = {1:1, 2:2, 3:3, 4:4}
print (dict, ‘ length is ‘, len (dict))

Output:
[]  length is  0
{1: 1, 2: 2, 3: 3, 4: 4}  length is  4

update():

The update() method adds element(s) to the dictionary if the key is not in the dictionary. If the key is in the dictionary, it updates the key with the new value.

यदि डिक्शनरी में की नहीं है, तो update() मेथड डिक्शनरी में एलीमेंट्स जोड़ता है। यदि की डिक्शनरी में है, तो यह नए | वैल्यू के साथ की को अपडेट करता है।

Syntax: dict.update([other])

d1 = {1: “100”, 2 : “300”}
d2 = {2: “200”}
d1. update (d2)
print (d1)
d3 = {3: “three”}
d1. update (d3)
print(d1)

Output:
{1: ‘100’, 2: ‘200’}
{1: ‘100’, 2: ‘200’, 3: ‘three’}

cmp(): It compare values and keys of two dictionaries, and returns Boolean value.

दो डिक्शनरी के वैल्यू और की की तुलना करें, और बूलियन वैल्यू पर रिटर्न हों।

Syntax: cmp(dict1, dict2)

d1 = {1: “100”, 2: “200”}
d2 = {1: “200”, 3: “300”}
d3 = {1: “100”, 2: “200” }
print(d1== d2)
print (d1== d3)

Output:
False # both are not equal
True # both are equal

Python Dictionary Comprehension
पायथन डिक्शनरी कांप्रिहेंसन

In Python, comprehension concept is applicable for dictionaries. Dictionary comprehension is an elegant and concise way to create new dictionary from an iterable in Python.

पायथन में, डिक्शनरी के लिए कांप्रिहेंसन का कॉन्सेप्ट लागू है। डिक्शनरी कांप्रिहेंसन पायथन में एक पुनरावृत्त से नया डिक्शनरी बनाने के लिए एक अच्छा और संक्षिप्त तरीका है। ..

Example:

dict = {x : x*x for x in range (1,10)}
print(‘x: x*x : ‘, dict)
dict = {x : 10*x for x in range (1,10)}
print (‘x : 10*x : ‘, dict)
dict = {x: x**x for x in range (1,10)}
print (‘x : x**x : ‘, dict)

Output:
x: x*x :  {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
x : 10*x :  {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60, 7: 70, 8: 80, 9: 90}
x : x**x :  {1: 1, 2: 4, 3: 27, 4: 256, 5: 3125, 6: 46656, 7: 823543, 8: 16777216, 9: 387420489}

Chapter wise Model Paper Link

About Me