เมื่อต้องการรวมสอง Dictionary เป็นเอนทิตี้เดียว สามารถใช้เมธอด 'update' ได้
Dictionary เป็น 'key-value' pair
ตัวอย่าง
my_dict_1 = {'J': 12, 'W': 22}
my_dict_2 = {'M': 67}
print("The first dictionary is :")
print(my_dict_1)
print("The second dictionary is :")
print(my_dict_2)
my_dict_1.update(my_dict_2)
print("The concatenated dictionary is :")
print(my_dict_1)
ผลลัพธ์
The first dictionary is : {'J': 12, 'W': 22}
The second dictionary is : {'M': 67}
The concatenated dictionary is : {'J': 12, 'W': 22, 'M': 67}
คำอธิบาย
กำหนดสอง Dictionary และแสดงบนคอนโซล
- เรียกเมธอด 'update' บน Dictionary แรก โดยส่ง Dictionary ที่สองเป็นพารามิเตอร์
- จะช่วยรวม Dictionary
- แสดงบนคอนโซล