maryo
maryo

Reputation: 23

How simplify my table when I'm creating it in Python

I want to simplify my code, like I have "Asics Gel 2000" two times, and when I'm creating my table I want to add it with a multiplicator *2 ! Look the example:

catalogue_tableau = ["Asics Gel 2000", "Asics Gel 2000", "Mizuno Wave rider", "Nike Air zoom", "Mizuno Wave plus", "Mizuno Wave plus", "Mizuno Wave plus", "Merrell Poseidon"]

And I would like to simplify like this:

catalogue_tableau = ["Asics Gel 2000"]*2, "Mizuno Wave rider", "Nike Air zoom", ["Mizuno Wave plus"] * 3,"Merrell Poseidon"]

But It's false and this isn't working. Somebody can help me ?

Upvotes: 0

Views: 43

Answers (2)

Shailyn Ortiz
Shailyn Ortiz

Reputation: 766

you can do it as follows:

catalogue_tableau = ["Asics Gel 2000"]*2 + [ "Mizuno Wave rider", "Nike Air zoom"] + ["Mizuno Wave plus"] * 3 + ["Merrell Poseidon"]

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1124758

You created a tuple with multiple strings and a few lists.

You can use the * iterable unpacking syntax inside a [...] list display, instead:

catalogue_tableau = [
    *(["Asics Gel 2000"] * 2),
    "Mizuno Wave rider", "Nike Air zoom",
    *(["Mizuno Wave plus"] * 3),
    "Merrell Poseidon"
]

The expression inside each *(...) group is expected to contain an iterable, whose values are added to the list in that location.

Upvotes: 3

Related Questions