How do you print two lists in python?
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,88011 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 289k60 gold badges603 silver badges675 bronze badges 3 No need to use
Your use of the answered Aug 20, 2012 at 15:55
LanaruLanaru 8,9127 gold badges33 silver badges62 bronze badges If you want to print
Do:
answered Aug 20, 2012 at 15:56
inspectorG4dgetinspectorG4dget 106k25 gold badges137 silver badges235 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 60.1k18 gold badges127 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,5571 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 I print two lists of elements in Python?Algorithm. Step1 : create two user input lists. Step2 : Convert the lists to sets and then print set1&set2. Step3 : set1 and set2 returns the common elements set, where set1 is the list1 and set2 is the list2.
How do you write two lists in Python?Method 2: Add two list using the Comprehension List. # initialize the Python lists.. lt1 = [2, 4, 6, 8, 10, 30]. lt2 = [2, 4, 6, 8, 10, 12]. # print the original list element.. print ( " Python list 1 : " + str (lt1)). print ( "Python list 2 : " + str (lt2)). # use list comprehension to add two lists.. How do I print a list list in Python?Printing a list in python can be done is following ways: Using for loop : Traverse from 0 to len(list) and print all elements of the list one by one using a for loop, this is the standard practice of doing it.
How do I print side by side in Python?Using "end" Argument in the print statement (Python 3. X) In Python 3, print() is a function that prints output on different lines, every time you use the function. ... . Using "sys" Library (Python 3. X) to Print Without Newline. ... . Using "comma" to Terminate Print Statement. In order to print in the same line in Python 2.. |