How do I combine values of two lists as one in Python?
Question:
Given these lists:
arr1 = ['A', 'B', 'C', 'D', 'E']
arr2 = [1, 1, 1, 1, 1]
What I want is to combine both of the lists as a single value.
Expected output:
1A1B1C1D1E
Answers:
Try:
''.join([str(b)+a for a,b in zip(arr1, arr2)])
'1A1B1C1D1E' # Output
It uses list comprehension to combine the string in arr1 to the int converted to string in arr2 and then joins them all together as one string.
arr1=['A', 'B', 'C', 'D', 'E']
arr2=[1, 1, 1, 1, 1]
S = []
for _ in range(len(arr1)):
S.append(arr1[_])
S.append(arr2[_])
print(S)
Given these lists:
arr1 = ['A', 'B', 'C', 'D', 'E']
arr2 = [1, 1, 1, 1, 1]
What I want is to combine both of the lists as a single value.
Expected output:
1A1B1C1D1E
Try:
''.join([str(b)+a for a,b in zip(arr1, arr2)])
'1A1B1C1D1E' # Output
It uses list comprehension to combine the string in arr1 to the int converted to string in arr2 and then joins them all together as one string.
arr1=['A', 'B', 'C', 'D', 'E']
arr2=[1, 1, 1, 1, 1]
S = []
for _ in range(len(arr1)):
S.append(arr1[_])
S.append(arr2[_])
print(S)