I'm trying to output the values of 2 lists side by side using list comprehension. I have an example below that shows what I'm trying to accomplish. Is this possible? Show
code:
asked Jan 1, 2018 at 23:04
qwiqwiqwiqwiqwiqwiqwiqwiqwiqwi 1371 gold badge3 silver badges11 bronze badges 2 You can use the
The Finally, just
answered Jan 1, 2018 at 23:11
6 This works:
Outputs:
answered Jan 1, 2018 at 23:11
Ivan86Ivan86 5,6372 gold badges13 silver badges30 bronze badges 0
answered Jan 1, 2018 at 23:18
Ronie MartinezRonie Martinez 1,2161 gold badge10 silver badges14 bronze badges
Note the square brackets enclosing the print statement. answered Apr 27 at 19:12
PeJotaPeJota 3133 silver badges8 bronze badges 1 Explore endless possibilities of printing and formatting lists in Python Python’s list data structure is built for simplicity and flexibility. We are going to have a look at how anyone can leverage lists as an essential tool for automation and sailing through tedious tasks. I’ll be showing several different techniques for printing a list in Python. I’ll cover the basics of printing a list using Python’s built-in Don't feel like reading? Watch my video instead: What is a List in Python?One of the most important features of any programming language is the ability to define variables. Variables allow the programmer to store information, like numbers or strings of text, and reuse it efficiently. This is part of what makes computers such terrific tools; we can feed them a bunch of information, and they can remember it and manipulate it way more easily than humans can. Like variables, Python lists also store information. More specifically, they store sequences of information. This is not unlike analog lists that you may be familiar with. If a person wants to store a sequence of to-do’s so they’ll remember them throughout the day, they may write a to-do list. Grocery lists are another example. While array and list structures in some programming languages require learning funky syntax and have strict rules about what data types they can hold, Python lists have none of that! This makes them super simple to use and equally flexible. Now we’ll take a look at how to define lists and access the information within them. Check it out:
Here, I have defined a list called If I want to access any of this information specifically, I would use a technique called list indexing. With list indexing, one simply puts the
number (starting with 0) of the element one wishes to access in square braces following the name of the list. For instance, if I want to print the first and third elements of
Output: Image 1 - Printing individual list elements in Python (image by author)Notice how I use the indices 0 and 2 to represent the first and third elements since Python indexing begins with 0. Now, that’s certainly useful, but you might be wondering how any of this is more efficient than working with normal variables. After all, we had to specify each element we wanted to print individually in the example above. Next up, I am going to show you how you can start to leverage some of Python’s other built-in features to access information in lists much more efficiently. Print a Python List with a For LoopPerhaps one of the most intuitive ways to access and manipulate the information in a list is with a for loop. What makes this technique so intuitive is that it reads less like programming and more like written English! Imagine you want to describe your intention of buying the items on your grocery list to a friend. You might say something like, “For each item in my grocery list, I want to buy this quantity”. You can write almost the same thing in Python to loop through your Python list. Let’s print some formatted strings based on the contents of our Python list!
Output: Image 2 - Printing a Python list in a for loop (image by author)Here, you can see that I was able to loop through each element of my list without having to specify each element manually. Imagine how much work this would save if your list had 50,000 elements instead of just three! You can also loop through a list using a for loop in conjunction with the built-in Python
The output here is the same, but instead of Now, we are getting somewhere. Hopefully, you can see that Python provides some great tools for accessing sequences of information stored in lists. Let’s take a look at some other ways to print Python lists! Use * Operator to Print a Python ListAnother way
to print all of the contents of a list is to use the Let’s see what that looks like in code:
Output: Image 3 - Printing a Python list with the * operator (image by author)It’s that simple! Print a List with the print() MethodIf you want a technique that’s even easier to use than that, you can simply pass the name of the list to the
Output: Image 4 - Printing a Python list with the print() method (image by author)The output in this case is a bit more "raw". We still have the square brackets around the list and the quotation marks around each element. Print a List with the map() MethodYet another way to print the elements of a list would be to use the Python built-in
Output: Image 5 - Printing a list with the map() method (image by author)You can ignore the Print a List with the join() MethodAnother handy way to print a Python list is to use the
Output: Image 6 - Printing a Python list with the join() method (image by author)You can see in the code that we are using our good friend, the Print a List on a Single Line with a Custom SeparatorAs I mentioned in the last section, we can also use the
Output: Image 7 - Printing a list with a custom separator (image by author)You can see that instead of joining our list elements with a space, we instead join them together with our custom separator Advanced: How to Print a List Vertically in Python?There are some advanced use cases in which you might have a list of lists, and you want to print list items vertically. It's easy to do with a single list, but not so intuitive with more complex data structures. Examine the following list - it contains three lists, each representing a score by three students on the test (3 tests in total):
To get scores of the first student, we could access the elements with the following syntax:
But that's tedious to write and isn't scalable. Instead, we could use two for loops - the first one to grab the three lists, and the second one to grab individual list items. You can then call the
Output: Image 8 - Printing Python lists vertically (image by author)You can further simplify the syntax by using the
The output is identical, but the code is significantly shorter. ConclusionI hope after these examples that you have a better understanding of the myriad ways that Python offers to access the information stored within a Python list. These techniques give the programmer tons of control over the formatting, and with a little know-how, you can get the resulting output that you want in as little as one line of code! Python does an exceptional job making tasks like this incredibly simple, and, at times, as easy as writing in English. Make sure to stay tuned for more in-depth Python tutorials! Recommended reads
Stay connected
How do I print a list of elements side by side 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 I print a separate list in Python?Without using loops: * symbol is use to print the list elements in a single line with space. To print all elements in new lines or separated by space use sep=”\n” or sep=”, ” respectively.
How do you print elements of a list in a single line in Python?When you wish to print the list elements in a single line with the spaces in between, you can make use of the "*" operator for the same. Using this operator, you can print all the elements of the list in a new separate line with spaces in between every element using sep attribute such as sep=”/n” or sep=”,”.
How do I print a list list in Python?How to Print a List of List in Python?. Short answer: To print a list of lists in Python without brackets and aligned columns, use the ''.join() function and a generator expression to fill each string with enough whitespaces so that the columns align:. Problem: Given a list of lists, print it one row per line.. |