Python Forum
Space between list and column alignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Space between list and column alignment
#1
Can anyone tell me how I can put more than one list in table style display please? With columns and rows are nicely aligned.

Thanks.
Reply
#2
Look back at your older Thread🧐
rturus likes this post
Reply
#3
(Mar-09-2021, 11:28 AM)snippsat Wrote: Look back at your older Thread🧐

However, my previous inquiry was for multi dimensional list.

This is time is separate list I am trying to figure out.
Reply
#4
Show example of input and wanted output.
Reply
#5
1: One way to display a data frame in the form of a table is by using the display() function of IPython.display.

filter_none
brightness_4
# importing the modules
from IPython.display import display
import pandas as PD

# creating a DataFrame
dict = {'Name' : ['Martha', 'Tim', 'Rob', 'Georgia'],
'Maths' : [87, 91, 97, 95],
'Science' : [83, 99, 84, 76]}
df = pd.DataFrame(dict)

# displaying the DataFrame
display(df)

2: In this example we’ll use DataFrame.style. It returns a Styler object, which has useful methods for formatting and displaying DataFrames.

filter_none
brightness_4
# importing the module
import pandas as pd

# creating a DataFrame
dict = {'Name' : ['Martha', 'Tim', 'Rob', 'Georgia'],
'Maths' : [87, 91, 97, 95],
'Science' : [83, 99, 84, 76]}
df = pd.DataFrame(dict)

# displaying the DataFrame
df.style
Reply
#6
(Mar-09-2021, 01:53 PM)snippsat Wrote: Show example of input and wanted output.

list_a=[1,2,3,4,5]
list_b=['d','h','f','s','g']
 
 
print(list_a)
print(list_b)
This is the output:
[1, 2, 3, 4, 5]
['d', 'h', 'f', 's', 'g']
Expected output:
[1, 2, 3, 4, 5 ]
['d', 'h', 'f', 's', 'g']
Reply
#7
(Mar-15-2021, 01:48 PM)rturus Wrote:
(Mar-09-2021, 01:53 PM)snippsat Wrote: Show example of input and wanted output.

list_a=[1,2,3,4,5]
list_b=['d','h','f','s','g']
 
 
print(list_a)
print(list_b)
This is the output:
[1, 2, 3, 4, 5]
['d', 'h', 'f', 's', 'g']
Expected output:
[1,     2,    3,    4,   5 ]
['d',  'h',  'f',  's', 'g']
Reply
#8
List is a data structure(internal use) and not meant to view like this,as that make no senseπŸ¦„
For view take elements out data structure,then can figure a different way to view it.
>>> list_a = [1,2,3,4,5]
>>> list_b = ['d','h','f','s','g']
>>> lst = []
>>> lst.append([str(i) for i in list_a])
>>> lst.append(list_b)
>>> 
>>> lst
[['1', '2', '3', '4', '5'], ['d', 'h', 'f', 's', 'g']]
>>> 
>>> for inner in lst:
...     print(' | '.join((f"{word:4}" for word in inner)))
...     
1    | 2    | 3    | 4    | 5   
d    | h    | f    | s    | g 
>>> from tabulate import tabulate
>>> 
>>> print(tabulate(lst, tablefmt="fancy_grid"))
╒═══╀═══╀═══╀═══╀═══╕
β”‚ 1 β”‚ 2 β”‚ 3 β”‚ 4 β”‚ 5 β”‚
β”œβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”€
β”‚ d β”‚ h β”‚ f β”‚ s β”‚ g β”‚
β•˜β•β•β•β•§β•β•β•β•§β•β•β•β•§β•β•β•β•§β•β•β•β•›
>>> print(tabulate(lst, tablefmt="pretty"))
+---+---+---+---+---+
| 1 | 2 | 3 | 4 | 5 |
| d | h | f | s | g |
+---+---+---+---+---+
>>> print(tabulate(lst, tablefmt="simple"))
-  -  -  -  -
1  2  3  4  5
d  h  f  s  g
-  -  -  -  -
>>> print(tabulate(lst, tablefmt="youtrack"))
|  1  |  2  |  3  |  4  |  5  |
|  d  |  h  |  f  |  s  |  g  |  
Reply
#9
(Mar-15-2021, 08:21 PM)snippsat Wrote: List is a data structure(internal use) and not meant to view like this,as that make no senseπŸ¦„
For view take elements out data structure,then can figure a different way to view it.
>>> list_a = [1,2,3,4,5]
>>> list_b = ['d','h','f','s','g']
>>> lst = []
>>> lst.append([str(i) for i in list_a])
>>> lst.append(list_b)
>>> 
>>> lst
[['1', '2', '3', '4', '5'], ['d', 'h', 'f', 's', 'g']]
>>> 
>>> for inner in lst:
...     print(' | '.join((f"{word:4}" for word in inner)))
...     
1    | 2    | 3    | 4    | 5   
d    | h    | f    | s    | g 
>>> from tabulate import tabulate
>>> 
>>> print(tabulate(lst, tablefmt="fancy_grid"))
╒═══╀═══╀═══╀═══╀═══╕
β”‚ 1 β”‚ 2 β”‚ 3 β”‚ 4 β”‚ 5 β”‚
β”œβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”€
β”‚ d β”‚ h β”‚ f β”‚ s β”‚ g β”‚
β•˜β•β•β•β•§β•β•β•β•§β•β•β•β•§β•β•β•β•§β•β•β•β•›
>>> print(tabulate(lst, tablefmt="pretty"))
+---+---+---+---+---+
| 1 | 2 | 3 | 4 | 5 |
| d | h | f | s | g |
+---+---+---+---+---+
>>> print(tabulate(lst, tablefmt="simple"))
-  -  -  -  -
1  2  3  4  5
d  h  f  s  g
-  -  -  -  -
>>> print(tabulate(lst, tablefmt="youtrack"))
|  1  |  2  |  3  |  4  |  5  |
|  d  |  h  |  f  |  s  |  g  |  

Thank you. This is very helpful.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code for alignment and font size 1418 0 322 Jan-14-2024, 03:56 AM
Last Post: 1418
  make all text in Right to left alignment in tkinter widgets jalal0034 1 1,337 Sep-27-2022, 06:42 PM
Last Post: Larz60+
  Right to left alignment in python report using Reportlab jalal0034 1 1,842 Sep-27-2022, 04:25 AM
Last Post: jalal0034
  pandas, tabulate, and alignment menator01 3 7,305 Feb-05-2022, 07:04 AM
Last Post: menator01
  Not able to add extra column to the list in the python shantanu97 2 1,658 Nov-17-2021, 10:14 AM
Last Post: snippsat
  Sort List of Lists by Column Nju 1 11,531 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  from global space to local space Skaperen 4 2,332 Sep-08-2020, 04:59 PM
Last Post: Skaperen
  Get Value from List to Show in DataFrame Column ahmedwaqas92 1 1,906 Jun-22-2020, 08:24 AM
Last Post: ahmedwaqas92
  list comprehension : print column as row pyseeker 4 3,669 Sep-05-2019, 05:40 AM
Last Post: pyseeker
  Inserting a python list into a dataframe column wise mahmoud899 0 4,285 Mar-04-2019, 11:44 PM
Last Post: mahmoud899

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020