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
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Brandon
  • 89
  • 7

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
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

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

Horia Coman
  • 8,681
  • 2
  • 23
  • 25