Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 26, 2022 02:28 pm GMT

{"Dictionary" : "fun"}

A dictionary is an unordered collection that consists of key-value pairs. Dictionaries are bounded by curly braces and have a list of key: value pairs which are separated by comma (,)

dict1 = {}dict1['name'] ='mouly'dict1['address'] = 'bogura'dict1['profession'] = 'student'print(dict1)

Output:

{'name': 'mouly', 'address': 'bogura', 'profession': 'student'}

Here, name, address, profession are keys. Keys need to be immutable type and keys are case sensitive.

Empty Dictionary

dict2 = {}print(dict2)

Output:

{}

Accessing a dictionary inside a dictionary

m_zen = { 88017:'rihan', 88015:'kiran', 88018 : ['sonam','riki'], 88013: 'harmeonie', 88016 :{24: 'chinal',26:'sonu'}}y = m_zen[88016][26]print(y)

Output:

sonu

Adding two list as key,value in dictionary

For this, we have to use zip function.

name = ['darla','remina','sonam','kiran']age = [23, 45, 3, 44]res = dict(zip(name,age))print(res)

Output:

{'darla': 23, 'remina': 45, 'sonam': 3, 'kiran': 44}

Length of a dictionary

store = {'bangla' : 3, 'english' : 4, 'german' : 5, 'arabic' : 2}print(len(store))

Output:

4

Dictionaries are mutable

Dictionaries are mutable while keys are immutable. So we can change the values of a dictionary.

dict3 = {'nila': 2345, 'mili': 2356, 'mona': 3456}y = dict3['mona']print(y)

Output:

3456

This is how we can access the value of a specific key of a dictionary.

We can also get the same value by using get function. The benefit of using get function is that, it does not give an error if we can not find value of a key; rather it gives None as output.

dict4 = {'nila': 2345, 'mili': 2356, 'mona': 3456}y = dict4.get('nila')print(y)

Output:

2345
dict4 = {'nila': 2345, 'mili': 2356, 'mona': 3456}#x = dict4['rita']                                     #if we print it, it will give a KeyErrory = dict4.get('rita')print(y)

Output:

None

changing the value of a dictionary

dictt = {'saturn': 2, 'moon': 1, 'mars': 'red'}print(dictt)dictt['moon'] = 'earth'print(dictt)

Output:

{'saturn': 2, 'moon': 1, 'mars': 'red'}{'saturn': 2, 'moon': 'earth', 'mars': 'red'}
address = {'room':3,'home':4,'street':2}print(address)address['room']= address['room']+ 2print(address)

Output:

{'room': 3, 'home': 4, 'street': 2}{'room': 5, 'home': 4, 'street': 2}

Here we can see how we the change value of a key of a dictionary.

Looping through a dictionary

songs = { 'red':'All Too Well', 1989: 'Style', 'reputation' : 'Gorgeous'}for i in songs.keys():    print(i)

Output:

red1989reputation
songs = { 'red':'All Too Well', 1989: 'Style', 'reputation' : 'Gorgeous'}for i in songs.values():    print(i)

Output:

All Too WellStyleGorgeous
songs = { 'red':'All Too Well', 1989: 'Style', 'reputation' : 'Gorgeous'}for i,j in songs.items():    print(i, j)

Output:

red All Too Well1989 Stylereputation Gorgeous

If I update a dictionary, then the updated list of keys will be displayed.

songs = { 'red':'All Too Well', 1989: 'Style', 'reputation' : 'Gorgeous'}y = songs.keys()songs['folklore'] = 'cardigan'print(y)

Output:

dict_keys(['red', 1989, 'reputation', 'folklore'])

Determining if a key exists in Dictionary

d = {'red':'All Too Well', 1989: 'Style', 'reputation' : 'Gorgeous'}if 'red' in d:    print('yup')

Output:

yup

Adding Elements

For adding a new element in a dictionary, we have to add a key and then assign a value in that key and it will add by default as the last value.

pdict = { 88017:'rihan', 'nila': 2345, 1989: 'Style', 88015:'kiran'}pdict['mouly'] = 'cse'print(pdict)

Output:

{88017: 'rihan', 'nila': 2345, 1989: 'Style', 88015: 'kiran', 'mouly': 'cse'}

Update method

Or, we can use update method.

pdict = { 88017:'rihan', 'nila': 2345, 1989: 'Style', 88015:'kiran'}pdict.update({'mouly' : 'cse'})print(pdict)

Output:

{88017: 'rihan', 'nila': 2345, 1989: 'Style', 88015: 'kiran', 'mouly': 'cse'}

Del method

We can use del method for removing an element or simply remove the whole dictionary.

rdict = {'darla': 23, 'remina': 45, 'sonam': 3, 'kiran': 44}del rdictprint(rdict)

Output:

NameError: name 'rdict' is not defined

For removing any selective element from a dictinary,

rdict = {'darla': 23, 'remina': 45, 'sonam': 3, 'kiran': 44}del rdict['sonam'] print(rdict)

Output:

{'darla': 23, 'remina': 45, 'kiran': 44}

Sorting dictionary

We can sort keys or values by using following method.

jdict = {'saturn': 'sat', 'moon': 'ear', 'mars': 'red', 'venus': 2}new = sorted(jdict.keys())print(jdict)print(new)

Output:

['mars', 'moon', 'saturn', 'venus']
m_zen = { 88017:'rihan', 88015:'kiran', 88018 : ['sonam','riki'], 88013: 'harmeonie', 88016 :{24: 'chinal',26:'sonu'}}y = sorted(m_zen.keys())print(y)

Output:

[88013, 88015, 88016, 88017, 88018]

Sorting values,

jdict = {'saturn': 'sat', 'moon': 'ear', 'mars': 'red', 'venus': 2}new = sorted(jdict.values())print(new)

Output:

TypeError: '<' not supported between instances of 'int' and 'str'

Important thing to notice, we can not compare between the instances of integer and string.

gdict = {'saturn': 'sat', 'moon': 'ear', 'mars': 'red', 'venus': 'blue'}new = sorted(gdict.values())print(new)

Output:

['blue', 'ear', 'red', 'sat']

Take a input dict from the user

n = 2d = dict(input("Enter: ").split() for i in range(n))        #while taking input write one key-value in only line and go to the next lineprint(d)

Output:

Enter: name moulyEnter: student bracu{'name': 'mouly', 'student': 'bracu'}

or,

val="""name moulyage 21home bogura"""y = dict(x.split() for x in val.splitlines())print(y)

Output:

{'name': 'mouly', 'age': '21', 'home': 'bogura'}

or,

n = int(input("enter a n value:"))d = {}for i in range(n):    keys = input()           # here i have taken keys as strings    values =input()          # here i have taken values as integers    d[keys] = valuesprint(d)

Output:

enter a n value:2namemoulyage21{'name': 'mouly', 'age': '21'}

or,

n = int(input())            #n is the number of items you want to enterd ={}                     for i in range(n):            text = input().split()     #split the input text based on space & store in the list 'text'    d[text[0]] = text[1]       #assign the 1st item to key and 2nd item to value of the dictionaryprint(d)

Output:

2name moulystreet 2{'name': 'mouly', 'street': '2'}

Original Link: https://dev.to/mouly22/dictionary-is-fun-1dh4

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To