-3
l1 = ["a", "b", "c"]
l2 = ["a", "b", "c"]

for i in l1:
    print(i)
    for i in l2:
        print(i)

Output: aabcbabccabc How can I get an output like this?: aabbcc

Dennis
  • 79
  • 1
  • 8

4 Answers4

2

You can use the zip builtin function. It will return the i-th item of all the iterables you pass. See zip doc

In your case:

for it in zip(l1, l2):
    # the * is for extracting the items from it
    # sep="" does not print any spaces between
    # end="" does avoid printing a new line at the end
    print(*it, sep="", end="")
Alexander Kosik
  • 669
  • 3
  • 10
2

Use zip:

l1 = ["a", "b", "c"]
l2 = ["a", "b", "c"]


for a, b in zip(l1, l2):
  print(a)
  print(b)
Ronie Martinez
  • 1,254
  • 1
  • 10
  • 14
0

If the arrays are the same size, you can do something like this:

for i in range((len(l1)):
    print(l1[i])
    print(l2[i])
jvieira88
  • 115
  • 7
0

We can use zip function as below

print(*(y for x in zip(l1,l2) for y in x))
hochae
  • 99
  • 4