All about dict in python

Programming Jun 2, 2017

dict is a data type in python language
dict have key and value which same is list in C#

create new dict

num = {‘one’: 1, ‘two’: 2, ‘three’: 3}

get dict size


get all dict key


get all dict value


add new data in dict

num[‘four’] = 4

print for each key and value in dict

for key, val in num.items():
  print (key, val)

update value in some key

num[‘one’] = ‘11’

delete dict


for sort key, output is list data type


for sort value, output is list data type


for sort by key and show all member in dict, output is tuples data type

sorted(num.items(), key=lambda x:x[1])

sort dict by value python
Assume that I have a dict. data = {1:‘b’, 2:‘a’}And I want to sort data by ‘b’ and ‘a’ so I get the result ‘a’,‘b’How do I do that?Any ideas?


Minseo Chayabanjonglerd

I am a full-time Android Developer and part-time contributor with developer community and web3 world, who believe people have hard skills and soft skills to up-skill to da moon.