The list corresponds to the same length Python3 List summing element

We assume there are three identical length list below:

uppercase_letters = ['A','B','C','D']
lowercase_letters = ['a','b','c','d']
digit = [1,2,3,4]

 

Method 1: Use for loop, three lists corresponding to the position of the element added directly.

= added_list [] # list adding the final result 
for I in Range (0, len (uppercase_letters)):
  added_list.append(uppercase_letters[i] + lowercase_letters[i] + str(digit[i]))

result:

['Aa1', 'Bb2', 'Cc3', 'Dd4']

 

Method two:
If only two lists are added, then we can use the zip () function, the corresponding list element to achieve two functions added.

# Method two: 
added_list = [] # listing added last Results

# First before the two lists are added 
for X, Y in ZIP (uppercase_letters, lowercase_letters):
  z = x + y
  added_list .append(z)

result:

['Aa', 'Bb', 'Cc', 'Dd']

 

Guess you like

Origin www.cnblogs.com/daydayup-lin/p/11913466.html