I have two arrays:
a = ['a', 'b', 'c']
b = ['1', '2', '3']
how can I combine them in one array called let's say "c" and which have the following view:
c = ['a1', 'b2', 'c3']
you can use the the built-in functions map
and zip
with str.join
:
list(map(''.join, zip(a, b)))
output:
['a1', 'b2', 'c3']
zip
will allow you to match the corresponding indexes:
result = [''.join(x) for x in zip(a,b)]
You can also try something like this ( Other than using zip function )
>>>[j + b[i] for i, j in enumerate(a)]
['a1', 'b2', 'c3']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.