a = ['a', 'b', 'c,']
b = [1, 2, 3]
c = [1, 2, 3]
#this is my effort on printing 3 lists side by side; however, I noticed it is completely wrong
res = "\n".join("{} {}".format(x, y, z) for x, y, z in zip(a, b, c))
print(res)
#how i want my results to look like a 1 1 b 2 2 c 3 3
I was expecting to print 3 lists side by side
CodePudding user response:
a = ['a', 'b', 'c']
b = [1, 2, 3]
c = [1, 2, 3]
i = 0
while i < len(a) and i < len(b) and i < len(c):
print(a[i], end=' ')
print(b[i], end=' ')
print(c[i], end=' ')
i = 1
CodePudding user response:
You can use the str.format() method to print multiple lists side by side in Python. The str.format() method allows you to specify placeholders in a string, and then fill those placeholders with values from variables.
To print the lists a, b, and c side by side, you can use the following code:
a = ['a', 'b', 'c']
b = [1, 2, 3]
c = [1, 2, 3]
# Create a format string with three placeholders for the values from the lists
format_string = "{:<4}{:<4}{:<4}"
# Print the lists using the format string
print(format_string.format("a", "b", "c"))
for x, y, z in zip(a, b, c):
print(format_string.format(x, y, z))
This code will produce the following output:
a b c
a 1 1
b 2 2
c 3 3
The format_string variable contains a format string with three placeholders for the values from the lists. The {:<4} placeholder specifies that the value should be left-aligned in a field of width 4. The zip() function is used to combine the values from the lists into tuples, and then the print() function is used to print each tuple using the format string.
Hope it helps.
Marcell