Home > OS >  Go through multiple lists and add them to a row in order using rich
Go through multiple lists and add them to a row in order using rich

Time:10-19

I have these lists that I want to go over and add each one to a row in order, for example:

gladiators, 1993, $140,863,596

I tried using a list comprehension and it didn't work.

from rich.console import Console
from rich.table import Table

table = Table(title="Star Wars Movies")
console = Console()

movie = [
    "gladiators",
    "war never changes",
    "the matrix",
    "hatred",
    "minions",
    "minions 2",
]

date = [
    "1993",
    "2005",
    "2015",
    "1984",
    "1999",
    "1978"
]

money = [
    "$140,863,596",
    "$197,873,756",
    "$147,673,496",
    "$182,173,376",
    "$195,673,846",
    "$136,273,946"
]

table.add_column("Released", justify="left", style="cyan", no_wrap=True)
table.add_column("Title", style="green")
table.add_column("Box Office", justify="right", style="red")

table.add_row(movie, money, date)

console.print(table)

CodePudding user response:

You can use zip like that:

for mov, mon, d in zip(movie, money, date):
    table.add_row(mov, mon, d)

console.print(table)

           Star Wars Movies                 
┏━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Released          ┃ Title        ┃ Box Office ┃
┡━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ gladiators        │ $140,863,596 │       1993 │
│ war never changes │ $197,873,756 │       2005 │
│ the matrix        │ $147,673,496 │       2015 │
│ hatred            │ $182,173,376 │       1984 │
│ minions           │ $195,673,846 │       1999 │
│ minions 2         │ $136,273,946 │       1978 │
└───────────────────┴──────────────┴────────────┘
  • Related