Brandon
Brandon

Reputation: 89

use .append more than once?

Is there a quicker way to write .append more than once? Like:

    combo.append(x) * 2

Instead of:

    combo.append(x)
    combo.append(x)

I know you cant multiply it by 2, so I'm wondering if there is a faster way?

Upvotes: 0

Views: 285

Answers (2)

Horia Coman
Horia Coman

Reputation: 8781

You might try something like combo.extend([x]*t) to append t copies of x.

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1123440

You can extend instead of append:

combo.extend([x] * 2)

If combo is a local or a global, you can also use += to extend a list:

combo += [x] * 2

If combo is an class attribute, just be careful if you are referencing it on an instance, see Why does += behave unexpectedly on lists?

Upvotes: 1

Related Questions