-2

Must be a very straight forward way of doing this - I have a tuple and I want to create a tuple of this tuple but with x number of repetitions, eg 4 below. Alternatively I can have a list of tuples and convert to tuple from there.

base_tuple = (0,1)
num_tuples = 4

output I want

((0,1),(0,1),(0,1),(0,1))

Thanks

Barmar
  • 741,623
  • 53
  • 500
  • 612

3 Answers3

4

Use the * operator to make repetitions of a tuple.

base_tuple = (0,1)
num_tuples = 4
result = (base_tuple,) * num_tuples
Barmar
  • 741,623
  • 53
  • 500
  • 612
1
>>> base_tuple = (0, 1)
>>> num_tuples = 4
>>> tuple([base_tuple] * num_tuples)
((0, 1), (0, 1), (0, 1), (0, 1))
Andrew Eckart
  • 1,618
  • 9
  • 15
1

Try

output = tuple(base_tuple for _ in range(4))
Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50