Add another tuple to a tuple of tuples
Asked Answered
A

6

72

I have the following tuple of tuples:

my_choices=(
         ('1','first choice'),
         ('2','second choice'),
         ('3','third choice')
)

and I want to add another tuple to the start of it

another_choice = ('0', 'zero choice')

How can I do this?

the result would be:

final_choices=(
             ('0', 'zero choice')
             ('1','first choice'),
             ('2','second choice'),
             ('3','third choice')
    )
Algorithm answered 19/8, 2010 at 14:56 Comment(1)
That's a tuple of tuples, not a list of tuples.Kostival
C
78

Build another tuple-of-tuples out of another_choice, then concatenate:

final_choices = (another_choice,) + my_choices

Alternately, consider making my_choices a list-of-tuples instead of a tuple-of-tuples by using square brackets instead of parenthesis:

my_choices=[
     ('1','first choice'),
     ('2','second choice'),
     ('3','third choice')
]

Then you could simply do:

my_choices.insert(0, another_choice)
Canvasback answered 19/8, 2010 at 15:4 Comment(1)
"Alternately, consider making my_choices a list-of-tuples", the reason being lists are mutable, contrary to tuples. And if tuples are immutable, it's because this allows more efficient operations.Chappell
W
34

Don't convert to a list and back, it's needless overhead. + concatenates tuples.

>>> foo = ((1,),(2,),(3,))
>>> foo = ((0,),) + foo
>>> foo
((0,), (1,), (2,), (3,))
Wanda answered 19/8, 2010 at 15:5 Comment(3)
I thought you couldn't change tuples?Traitorous
You can't---but you can redefine them :)Wanda
You can't---but you can redefine them, actually what you're doing is assigning a new tuple object to the existing variable foo (which happens to already hold another tuple object, but that's irrelevant). So we need to understand "redefining" as a trivial assignment. +1Chappell
B
8

Alternatively, use the tuple concatenation

i.e.


final_choices = (another_choice,) + my_choices
Bullivant answered 19/8, 2010 at 15:5 Comment(0)
M
3

You could use the "unpacking operator" (*) to create a new tuple

tpl = ((2, 11), (3, 10),)

tpl = (*tpl, (5, 8))

print(tpl) #prints ((2, 11), (3, 10), (5, 8))

Masaryk answered 6/8, 2022 at 9:13 Comment(0)
A
2

What you have is a tuple of tuples, not a list of tuples. Tuples are read only. Start with a list instead.

>>> my_choices=[
...          ('1','first choice'),
...          ('2','second choice'),
...          ('3','third choice')
... ]
>>> my_choices.insert(0,(0,"another choice"))
>>> my_choices
[(0, 'another choice'), ('1', 'first choice'), ('2', 'second choice'), ('3', 'third choice')]

list.insert(ind,obj) inserts obj at the provided index within a list... allowing you to shove any arbitrary object in any position within the list.

Auspicate answered 19/8, 2010 at 15:3 Comment(0)
B
0
my_choices=(
         ('1','first choice'),
         ('2','second choice'),
         ('3','third choice')
)

new=('0','zero choice')

l=[item for item in my_choices]
l.insert(0,new)
my_choices=tuple(l)
print(my_choices)
  1. create a list from element of my_choices tuple
  2. Now you can insert the new tuple element at the first position (index 0)
  3. Create a new tuple from list named list and assign it to "my_choices"
  4. Print my_choices variable (a tuple of tuples)
Bellwether answered 29/12, 2022 at 13:0 Comment(1)
This answer was reviewed in the Low Quality Queue. Here are some guidelines for How do I write a good answer?. Code only answers are not considered good answers, and are likely to be downvoted and/or deleted because they are less useful to a community of learners. Please edit your answer to include an explanation of how and why the code solves the problem, when it should be used, what its limitations are, and if possible a link to relevant documentation.Catcher

© 2022 - 2024 — McMap. All rights reserved.