16

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

2
  • 1
    if this is loop will be performance critical, you should .append into a list then use tuple(a) at the end. Commented Feb 17, 2018 at 2:57
  • @juanpa.arrivillaga. or better yet, a deque Commented Feb 17, 2018 at 6:11

6 Answers 6

17

Use an extra comma in your tuples, and just join:

a = ((1,1,1),)
for i in range(2,10):
    a = a + ((i,i,i),)

Edit: Adapting juanpa.arrivillaga's comment, if you want to stick with a loop, this is the right solution:

a = [(1,1,1)]
for i in range (2,10):
    a.append((i,i,i))
a = tuple(a)   
Sign up to request clarification or add additional context in comments.

3 Comments

It will work, but seems like a terrible way to do it.
First case could start with empty tuple and have the range start with 1
Thank you, the true it is that I din't want exactly the tuple I posted, what I needed was to append in a tuple an x,y,z Choords of an array (Nx3), like this way ((x1,y1,z1),(x2,y2,z2)....(xn,yn,zn)) So, you script, it helps a lot.
16

In this case, you can create it without having to use a loop.

a = tuple((i,)*3 for i in range(1, 10))

Comments

5

itertools.repeat can also be used here:

>>> from itertools import repeat
>>> [tuple(repeat(i, 3)) for i in range(1, 10)]
[(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)]

If you want the final result to be in a tuple of tuples instead of a list of tuples, you can wrap tuple again:

>>> tuple(tuple(repeat(i, 3)) for i in range(1, 10))
((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))

Comments

3

If I were to imitate something like this, I would have done it in the following way:

a = tuple((n,n,n) for n in range(1,10))
print(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))

This is the most simple and pythonic way to do this specific job.

Comments

3

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:

  1. Convert the tuple into a list
  2. Make the necessary changes to the list
  3. Convert the list back to a tuple

or

  1. Create a list
  2. Modify the list
  3. Convert the list into a tuple

So, in your case:

a = []
for i in range (1,10):
    a.append((i,i,i))
a = tuple(a)   
print a

Comments

-2

A little experimentation got this working. I guess you need a comma after the tuple in a to convince python it is a tuple.

a = ((1,1,1),)
for i in range(2, 10):
  a = a + ((i,i,i),)

print(a)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.