0

I have two lists, and want to combine them into one, by each element:

List1 = ['s', 'd', 'c']
List2 = ['a', 'b', 'h']

What I would like:

List = ['sa', 'db', 'ch']

3 Answers3

2

Try this:

[a+b for a, b in zip(List1, List2)]
yvesonline
  • 4,609
  • 2
  • 21
  • 32
0

List comprehension and zip are the savior for you. Try:

[''.join(i) for i in zip(List1, List2)]

OR you can do:

[i + j for i, j in zip(List1, List2)]
abhiarora
  • 9,743
  • 5
  • 32
  • 57
-1
List1 = ['s', 'd', 'c']
List2 = ['a', 'b', 'h']
ls =[]
for i in range(len(List1)):
    ls.append(List1[i]+List2[i])

print(ls)
Sohaib
  • 566
  • 6
  • 15
  • Try not to use indices for looping and consider using `enumerate` if you really have to. The loop can be avoided with a list comprehension; it's more efficient. – Koto Mar 29 '20 at 11:28
  • thank you for your very good suggestion. I am new to python and on a way to learning. Hopefully next time will be careful about it. – Sohaib Mar 29 '20 at 11:30