I want to create this tuple:
a=(1,1,1),(2,2,2),(3,3,3),(4,4,4),(5,5,5),(6,6,6),(7,7,7),(8,8,8),(9,9,9)
I tried with this
a=1,1,1
for i in range (2,10):
a=a,(i,i,i)
However it creates a tuple inside other tuple in each iteration.
Thank you
Use an extra comma in your tuples, and just join:
Edit: Adapting juanpa.arrivillaga's comment, if you want to stick with a loop, this is the right solution:
A little experimentation got this working. I guess you need a comma after the tuple in a to convince python it is a tuple.
itertools.repeat
can also be used here:If you want the final result to be in a tuple of tuples instead of a list of tuples, you can wrap
tuple
again:A tuple is an immutable list. This means that, once you create a tuple, it cannot be modified. Read more about tuples and other sequential data types here.
So, if you really need to change a tuple during run time:
or
So, in your case:
You can declare it without having to use a loop.
If I were to imitate something like this, I would have done it in the following way:
This is the most simple and pythonic way to do this specific job.