Change language

Python – Print list vertically

The printout of the list has been processed several times. But sometimes we need a different format to get the output of list. This also has an application to achieve matrix transposition. Vertical list printing also has application in web development. Let’s discuss some ways this task can be accomplished.

Method # 1: Using zip ()

Using the zip function, we map the elements at the respective index to each other and after that we print each of them. This performs the vertical print job.

# Python3 code to demonstrate
# Vertical list print
# using zip()

# initializing list
test_list = [[1, 4, 5], [4, 6, 8], [8, 3, 10]]

# printing original list
print ("The original list is : " + str(test_list))

# using zip()
# to print list vertically
for x, y, z in zip(*test_list):
	print(x, y, z)

Output:

The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]]
1 4 8
4 6 3
5 8 10

Method # 2: Using the Naive Method

The naive method can be used to print the list vertically screw. using loops and printing each index item in each list in succession will help us accomplish this task.

# Python3 code to demonstrate
# Vertical list print
# using naive method

# initializing list
test_list = [[1, 4, 5], [4, 6, 8], [8, 3, 10]]

# printing original list
print ("The original list is : " + str(test_list))

# using naive method
# to print list vertically
for i in range(len(test_list)):
	for x in test_list:
		print(x[i], end =’ ’)
	print()

Output:

The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]]
1 4 8 
4 6 3 
5 8 10 

How to display a list vertically?

StackOverflow question

I have a list of letters and want to be able to display them vertically like so:

a d
b e
c f

def main():
    letters = ["a", "b", "c", "d","e", "f"]
    for i in letters:
       print(i)

this code only display them like this:

a
b
c
d
e

Answer:

That’s because you’re printing them in separate lines. Although you haven’t given us enough info on how actually you want to print them, I can infer that you want the first half on the first column and the second half on the second colum.

Well, that is not that easy, you need to think ahead a little and realize that if you calculate the half of the list and keep it: h=len(letters)//2 you can iterate with variable i through the first half of the list and print in the same line letters[i] and letters[h+i] correct? Something like this:

def main():
    letters = ["a", "b", "c", "d","e", "f"]
    h = len(letters)//2 # integer division in Python 3
    for i in range(h):
       print(letters[i], letters[h+i])

You can easily generalize it for lists without pair length, but that really depends on what you want to do in that case.

That being said, by using Python you can go further :). Look at this code:

def main():
    letters = ["a", "b", "c", "d","e", "f"]
    for s1,s2 in zip(letters[:len(letters)//2], letters[len(letters)//2:]): #len(letters)/2 will work with every paired length list
       print(s1,s2)

This will output the following in Python 3:

a d
b e
c f

What I just did was form tuples with zip function grouping the two halves of the list.

For the sake of completeness, if someday your list hasn’t a pair length, you can use itertools.zip_longest which works more or less like zip but fills with a default value if both iterables aren’t of the same size.

Hope this helps!

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically