Adding Elements of Two Lists
Let’s say you have two lists and you want to merge them into one single list by adding their elements. It can be useful in some scenarios like
Python: 3.0    Abhay Parashar    04 Jul 2021   0
Login to upvote
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
list1 = ['karl','lary','keera']
list2 = [28934,28935,28936]

# Method 1: zip()
dictt0 = dict(zip(list1,list2))

# Method 2: dictionary comprehension
dictt1 = {key:value for key,value in zip(list1,list2)}

# Method 3: Using a For Loop (Not Recommended)
tuples = zip(list1, list2) 
dictt2 = {} 
for key, value in tuples: 
  if key in dictt2: 
    pass
  else: 
    dictt2[key] = value
print(dictt0, dictt1, dictt2, sep = "\n")
Copy | Download as: .py file   PNG
Share on: FB   Twitter   LinkedIn   WA
©2021 - code108labs   Contributors   Hosted on DigitalOcean   Partner website: pythoncircle.com