Blog

Dictionary in python - Bharosewale

Posted By Naveen Singh on 17 Mar 2023

python-program
python-me-dictionary-kya-hote-hai-in-hindi

Python में dictionary क्या है?(what are dictionary in python?)

Python में dictionary का use हम data values को store करने के लिए करते हैं, जो कि key:value pairs के form में होते है। Python में dictionary data elements का collection होता है, जो की ordered, changeable और duplicate value allow नही करता है।

Ordered-: ordered का मतलब है कि elements का किसी defined order में होना।

Changeable-: changeable का मतलब है कि dictionary को create करने के बाद हम उसमे changes कर सकते हैं।

Duplicate not allowed-:  dictionary में हम दो element को same name से create नही कर सकते है।

Example-:

mydict = {

  "brand": "Ford",

  "model": "Mustang",

  "year": 1978,

  "year": 2022

}

print(mydict)

Output-:

{'brand': 'Ford', 'model': 'Mustang', 'year': 2022}


 

Python में dictionary को create कैसे करें?(how to create dictionary in python?)

Python में हम dictionary को  curly brackets {} के अन्दर key:value pair के form में लिखते है।

Example-:

mydict = {

  "name": "shivam",

  "course": "o level",

  "year": 2022

}

print(mydict)

Output-:

{'name': 'shivam', 'course': 'o level', 'year': 2022}

Dictionary में हम different data type का प्रयोग कर सकते हैं।


 

Python में dictionary को access कैसे करें?(how to access dictionary in python?)

Python में हम key name के through element को access कर सकते हैं, और Key name square bracket [] के अन्दर होना चाहिए।

Example-:

mydict = {

  "name": "naveen",

  "course": "python",

  "year": 2022

}

x = mydict["course"]

Output-:

python

get()-: इस function की भी सहायता से element को access कर सकते जो की same result provide करता है।

Example-:

mydict = {

  "name": "naveen",

  "course": "python",

  "year": 2022

}

x = mydict.get("coursel")

Output-:

python


 

Python में dictionary की values को change कैसे करे?(how to change values of dictionary in python?)

Dictionary में  हम key name के through किसी भी specific element की value को change कर सकते हैं।

Example-:

newdict = {

  "name": "rohit",

  "location": "para",

  "year: 1999

}

newdict["year"] = 2018

print(newdict)

Output-

{'name': 'rohit', 'location': 'para', 'year': 2018}


 

Python में dictionary को update कैसे करे?(how to update dictionary in python?)

Python में हम update() function के through dictionary को update कर सकते हैं। जिसमे हमको argument पास करना होता है,  जो कि key:value pair form में होना चाहिए।

Example-:

mydict = {

  "name": "prince",

  "course": "android",

  "year": 1999

}

mydict.update({"year": 2020})

Output-:

{'name': 'prince', 'course': 'android', 'year': 2020}


 

Dictionary में elements को add कैसे करे?(how to add elements in dictionary?)

Python में हम dictionary को key:value pair के through add कर सकते हैं, जिसमे हमे new index key create करनी होती है और उसमे new value assign करनी होती है।

Example-:

mydict = {

  "name": "naveen",

  "course": "android",

  "year": 2022

}

mydict["location"] = "Lucknow "

print(thisdict)

Output-:

{'name': 'naveen', 'course': 'android', 'year': 2022, 'location': 'Lucknow'}

update()-: update () function के through भी हम dictionary में element को add कर सकते हैं, जिसमे हमे argument pass करनी होती है जो की key:value pair के form में होनी चाहिए।

Example-:

mydict = {

  "brand": "Ford",

  "model": "Mustang",

  "year": 1964

}

mydict.update({"color": "red"})

print(mydict)

Output-:

{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red'}


 

Dictionary में elements को remove कैसे करें?(how to remove elements in dictionary?)

Dictionary में हम elements को कई तरह remove कर सकते हैं-

pop()-: pop() function element को उनके specified key name की सहायता से remove करता है।

Example-:

newdict = {

 "name": "siddhant",

  "area": "kanpur",

  "course": "android"

}

newdict.pop("course")

print(mydict)

Output-:

{'name': 'siddhant', 'area':'kanpur'}

popitem()-: यह function dictionary के last inserted element को remove करता है।

Example-:

newdict = {

 "name": "siddhant",

  "area": "kanpur",

  "course": "android"

}

newdict.popitem()

print(mydict)

Output-:

{'name': 'siddhant', 'area':'kanpur'}

del-: del एक keyword है, जो की किसी specified key name की सहायता से element को remove करता है।

Example-:

newdict = {

 "name": "siddhant",

  "area": "kanpur",

  "course": "android"

}

del mydict["course"]

print(mydict)

Output-:

{'name': 'siddhant', 'area':'kanpur'}

clear()-: इस function की सहायता से हम dictionary को पूरी तरह से remove कर सकते है, मतलब की यह function dictionary को पूरी तरह से empty बना देता है।

Example-:

newdict = {

 "name": "siddhant",

  "area": "kanpur",

  "course": "android"

}

 mydict.clear()

print(mydict)

Output-:

{}


 

Python मे dictionary element को loop के through कैसे access करते हैं? (How to access dictionary element using loop in python?)

Python मे dictionary elements को हम for loop की सहायता से access कर सकते है।

Example-:

newdict = {

 "name": "siddhant",

  "area": "kanpur",

  "course": "android"

}

for a in mydict:

    print(a)

Output-:

name

area

course

इसकी सहायता से हम सर्फ keys को ही print कर सकते है।

Dictionary मे कुछ और function भी होते है जिनकी सहायता से हम value को भी print कर सकते है।

Example-:

newdict = {

 "name": "siddhant",

  "area": "kanpur",

  "course": "android"

}

for a in mydict:

   print(newdict[x])

Output-:

siddhant

kanpur

android



Python मे dictionary को copy कैसे करे?(how to add dictionary in python?)

आप केवल dict2 = dict1 लिखकर किसी dictionary की copy नहीं बना सकते, क्योंकि dict2  केवल  dict1 का reference होगा, और dict1 में किए गए changes automatically dict2 में भी किए जाएंगे।

Python में हम copy() function सहायता से हम dictionary को copy कर सकते हैं|

Example-:

newdict = {

 "name": "naveen",

  "area": "lucknow",

  "course": "android"

}

mydict = newdict.copy()

print(mydict)

Output-:

{'name': 'naveen', ‘area’: 'lucknow', 'course':’android’}

dict()-:  यह एक built-in function होता है।जिसकी सहायता से  हम dictionary को copy कर सकते है।

Example-:

newdict = {

 "name": "naveen",

  "area": "lucknow",

  "course": "android"

}

mydict = dict(newdict)

print(mydict)

Output-:

{'name': 'naveen', ‘area’: 'lucknow', 'course':’android’}


 

Python मे nested dictionary क्या होते है?(what is nested dictionary in python?)

जब हम किसी dictionary के अंदर dictionary को create करते है, तो  उसे हम nested dictionary कहते है।

Example-:

mystudent = {

  "stu1" : {

    "name" : "Emil",

    "course" : “python”

  },

  "stu2" : {

    "name" : "Tobias",

    "course" : “android”

  },

  "stu3" : {

    "name" : "Linus",

    "course" : “java”

  }

}

print(mystudent)

Output-:

{'stu1': {'name': 'Emil', 'course': ‘python’}, 'stu2': {'name': 'Tobias', 'course': ‘android’}, 'stu3': {'name': 'Linus', 'course': ’java’}}


 

Nested dictionary से elements को access कैसे करे?(how to access element in nested dictionary?)

Nested dictionary मे elements को dictionary के name के through access कर सकते है| जिसकी starting outer dictionary से होती है।

Example-:

mystudent = {

  "stu1" : {

    "name" : "Emil",

    "course" : “python”

  },

  "stu2" : {

    "name" : "Tobias",

    "course" : “android”

  },

  "stu3" : {

    "name" : "Linus",

    "course" : “java”

  }

}

print(myfamily["stu2"]["name"])

Output-:

Tobias


 

Python में Dictionary method कितने प्रकार के होते हैं समझाइए?(how many types of dictionary method in python explain?)

 

Python में कई तरह के built-in function होते हैं, जो कि हम dictionary में use करते है -

clear()- इस function की सहायता से हम dictionary में सारे elements को remove  कर सकते है।

Example-:

dict= {

  "name": "harrison",

  "location":"Washington",

  "year": 2021

}

dict.clear()

print(dict)

Output-:

{}

copy()-: इस function की सहायता से हम एक dictionary को दूसरी dictionary में copy कर सकते हैं।

Example-:

dict={

"name":"harrison",

"location":"Washington",

"year":2021

}

a = dict.copy()

print(a)

Output-:

{"name":"harrison","location":"Washington","year":2021}

fromkeys-: यह function एक specified key:value के form में dictionary को return करता है।

Example-:

dict1 = ('key1', 'key2', 'key3')

dict2 = 0

mydict =

dict.fromkeys(dict1, dict2)

Output-:

{'key1': 0, 'key2': 0, 'key3': 0}

get()-: इस function का use हम किसी specified key की value को return करने के लिए करते हैं।

Example-:

dict={

    "name": "harrison",

    "location": "Washington",

   "year":2021

  }

a=dict.get("location")

print(a)

Output-:

Washington

 

items-: यह function एक तरह की list create करता है जो एक tuple type की value को contain करके रखता है। जिसमे key:value pair form में value होती है।

Example-:

dict={

    "name": "harrison",

    "location": "Washington",

   "year":2021

  }

a=dict.items()

print(a)

Output-:

dict_items([('name', 'harrison'), ('location', 'Washington'), ('year', 2021)])

 

keys()-: यह function  dictionary में keys को return करता है।

Example-:

 dict = {

  "name": "harrison",

  "location": "Washington",

  "year": 2021

}

a = dict.keys()

print(a)

Output-:

dict_keys(['name', 'location', 'year'])

values()-: यह function dictionary में से values की list को return करता है।

Example-:

dict = {

  "name": "harrison",

  "location": "Washington",

  "year": 2021

}

a = dict.values()

print(a)

Output-:

dict_values(['harrison', 'Washington', 2021])


 

Conclusion
 

I Hope आपको Python में dictionary क्या होता है? dictionary python में कैसे work करता  है, और dictionary के functions के बारे में आपको जानकारी मिली होगी। यदि आपको कोई doubt है तो comment section में comment करके पूछ सकते हैं।

Comments