Search code examples
pythontuples

Add another tuple to a tuple of tuples


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')
    )

Solution

  • 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)