All about dict in python

Programming Jun 2, 2017
https://pixabay.com/en/dictionary-reference-book-learning-1619740/

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

len(num)

get all dict key

num.keys()

get all dict value

num.values()

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

num.clear()

https://docs.python.org/3/library/stdtypes.html#mapping-types-dict


for sort key, output is list data type

sorted(num)

for sort value, output is list data type

sorted(num.value)

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?

Tags

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.