How do i print two columns side by side in python?

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?

code:

#example lists, the real lists will either have less or more values
a = ['a', 'b', 'c,']
b = ['1', '0', '0']

str = ['``` \n'
       'results: \n\n'
       'options   votes \n'
       #this line is the part I need help with: list comprehension for the 2 lists to output the values as shown below
       '```']

print[str]

#what I want it to look like:
'''
results:

options  votes
a        1
b        0
c        0
''' 

asked Jan 1, 2018 at 23:04

qwiqwiqwiqwiqwiqwiqwiqwiqwiqwi

1371 gold badge3 silver badges11 bronze badges

2

You can use the zip[] function to join lists together.

a = ['a', 'b', 'c']
b = ['1', '0', '0']
res = "\n".join["{} {}".format[x, y] for x, y in zip[a, b]]

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.

print[res]
a 1
b 0
c 0

answered Jan 1, 2018 at 23:11

6

This works:

a = ['a', 'b', 'c']
b = ['1', '0', '0']

print["options  votes"]

for i in range[len[a]]:
    print[a[i] + '\t ' + b[i]]

Outputs:

options  votes
a        1
b        0
c        0

answered Jan 1, 2018 at 23:11

Ivan86Ivan86

5,6372 gold badges13 silver badges30 bronze badges

0

from __future__ import print_function  # if using Python 2

a = ['a', 'b', 'c']
b = ['1', '0', '0']

print["""results:

options\tvotes"""]

for x, y in zip[a, b]:
    print[x, y, sep='\t\t']

answered Jan 1, 2018 at 23:18

Ronie MartinezRonie Martinez

1,2161 gold badge10 silver badges14 bronze badges

[print[x,y] for x,y in zip[list1, list2]]

Note the square brackets enclosing the print statement.

answered Apr 27 at 19:12

PeJotaPeJota

3133 silver badges8 bronze badges

1

Project description

Print two outputs side-by-side, in Python

side_by_side allows users to print two multi-line outputs side-by-side. This produces an effect similar to running diff -y file1 file2 in a Unix system.

Usage

This library provides a single function, side_by_side.print_side_by_side. To print two_strings side-by-side with default settings:

from side_by_side import print_side_by_side

print_side_by_side[s1, s2]

Optional parameters

  • print_line_numbers [bool]: If True, prints line-numbers along the left column.
  • col_padding [int]: the number of spaces to leave between the two columns [and between the text and line number, if applicable]
  • delimiter [str]: a delimiter to separate the columns

Fancy usage

Here's an example of a print format that uses all of the parameters:

from side_by_side import print_side_by_side

print_side_by_side[lorem_ipsum, lorem_ipsum, print_line_numbers=True, col_padding=24, delimiter='|++++|']

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

How do you print two output 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..

How do I print something side by side?

Set up a printer to print to both sides of a sheet of paper.
Click the File tab..
Click Print..
Under Settings, click Print One Sided, and then click Manually Print on Both Sides. When you print, Word will prompt you to turn over the stack to feed the pages into the printer again..

How do you print lists next to each other 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 print columns in Python?

Print With Column Alignment in Python.
Use the % Formatting to Print With Column Alignment in Python..
Use the format[] Function to Print With Column Alignment in Python..
Use f-strings to Print With Column Alignment in Python..
Use the expandtabs[] Function to Print With Column Alignment in Python..

Chủ Đề