'print with two for loops in python
I have a question about python.
I need to print two variables stored in a list. example list1 = ["1", "2", "3"] list2 = ["a", "b", "c"]
output print: 1a, 2b, 3c
Solution 1:[1]
You want zip.
for i1, i2 in zip(list1, list2):
print(i1 + i2)
Solution 2:[2]
You can use zip() and .join() to produce the desired output:
list1 = ["1", "2", "3"]
list2 = ["a", "b", "c"]
# Prints 1a, 2b, 3c
print(', '.join(''.join(item) for item in zip(list1, list2)))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Samwise |
| Solution 2 | BrokenBenchmark |
