All about dict in python

programming Jun 02, 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

Android Developer ผู้เป็นเจ้าของบล็อก MikkiPastel ที่ชอบทำหลายๆอย่างนอกจากเขียนแอพแอนดรอยด์ เช่น เขียนบล็อก เขียนแชทบอท เรียนออนไลน์ อ่านหนังสือ วาดรูปเล่น ดู netfilx สั่งอาหารอร่อยๆกัน เป็นต้น

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.