Suppose I have 3 lists such as these Show
how do I get to print out everything from these lists at the same time ? What's the pythonic way to do something like that ?
This only seems to be taking 2 lists into account. Desired output: for each element in all the lists, I'm printing them out using a different function
and I Call the above function inside the for loop. asked Aug 20, 2012 at 15:52
cyberbemoncyberbemon 2,87011 gold badges36 silver badges61 bronze badges 5 I think you might want
What you're doing:
is a little strange. It is basically equivalent to If you just want to print each list consecutively, you can do:
answered Aug 20, 2012 at 15:54
mgilsonmgilson 288k60 gold badges601 silver badges674 bronze badges 3 No need to use
Your use of the answered Aug 20, 2012 at 15:55
LanaruLanaru 8,8827 gold badges33 silver badges62 bronze badges If you want to print
Do:
answered Aug 20, 2012 at 15:56
inspectorG4dgetinspectorG4dget 106k25 gold badges137 silver badges234 bronze badges It depends on what you want to achieve,
or the following implementation which will work in Python 2.7
answered Aug 20, 2012 at 16:09
AbhijitAbhijit 60k18 gold badges125 silver badges197 bronze badges It you're lists are not all the same length it is often better to use
answered Aug 20, 2012 at 16:06
Charles BeattieCharles Beattie 5,5471 gold badge29 silver badges32 bronze badges 1 To expand on top of Abhijit answer, you could use the itertools generator as the iterable within a list comprehension.
answered Aug 20, 2012 at 17:39
DanDan 1,1492 gold badges10 silver badges18 bronze badges If you mean that you have 3 lists of equal length and you want to print out their contents as 3 columns, then how about zip() to connect the columns and a list comprehension to print() on each iteration:
The above will print repr's of tuple(s). If you want to format the values otherwise:
Nobody said you have to use the output of a list comprehension.
answered Dec 20, 2017 at 19:28
pourhauspourhaus 5176 silver badges9 bronze badges How do you print two lists in Python?You can use the zip() function to join lists together. The zip() function will iterate tuples with the corresponding elements from each of the lists, which you can then format as Michael Butscher suggested in the comments. Finally, just join() them together with newlines and you have the string you want.
How do you merge two lists in Python?In python, we can use the + operator to merge the contents of two lists into a new list. For example, We can use + operator to merge two lists i.e. It returned a new concatenated lists, which contains the contents of both list_1 and list_2.
How do you show multiple lists in Python?With zip. The zip function brings together elements form each of the lists from the same index and then moves on to the next index. This type of appending is useful when you want to preserver the elements form the lists at the same index position together.
How do I print a list list in Python?4 Methods to Print List in Python. Using loops. When you come across printing the list while programming, the first thought that comes to your mind is to print it using the loops. ... . Using join() method. ... . Using the '*' symbol: ... . Using map(). |