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
Asked By: anonymous a

||

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.

Answered By: Michael S.
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)
Answered By: Bhaskar Gupta
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.