0

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?

Martijn Pieters
  • 963,270
  • 265
  • 3,804
  • 3,187
Brandon
  • 77
  • 6

2 Answers2

1

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?

Community
  • 1
  • 1
Martijn Pieters
  • 963,270
  • 265
  • 3,804
  • 3,187
1

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

Horia Coman
  • 8,442
  • 2
  • 21
  • 25