How to combine two dictionaries in Python

preview_player
Показать описание


Code from video:
*****************
employees = {1: 'michael', 2: 'eric', 3: 'michael'}
new_hires = {4: 'erick', 1: 'julia'}
employees

Connect with us!
*****************
Рекомендации по теме
Комментарии
Автор

this is updating not combining in the sense that you might want to retain the original value for the same key...

hiydavid
Автор

if i want to same key combine, how can i do ? like 1: {''julia', "michal"}

boonhonggoh
Автор

This is my code, please advise
itemno = 0

for item in root.iter('ItemDetails'):
itemno += 1
products.update(products)
d = dict()
LineType = item.find('LineType')
products['LineType'] = LineType.text

line_No = item.find('Line_No')
products['line_No'] = line_No.text
print(f'Line item {itemno} has {products}')
print(products)

Expected output:
{ 1: {'LineType': 'Product', 'line_No': '1'}, 2: {'LineType': 'Product', 'line_No': '2'} }

ashuleodubey