Home > Blockchain >  How do I combine values of two lists as one in Python?
How do I combine values of two lists as one in Python?

Time:08-22

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

CodePudding user response:

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.

CodePudding user response:

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)
  • Related