1

I am trying to convert list of tuples to tuple of tuples , what I am doing is wrong, Please help to fix this issue

list1= [('2', '297'), ('6', '297')]

for x in list1:
    print("inside fn")
    print(x)
    tuple_of_tuples+=tuple(x)
    print(tuple_of_tuples)

output = ('2', '297', '6', '297') I want output as (('2', '297'), ('6', '297'))

3
  • 1
    tuple(list1) should do Commented Mar 9, 2017 at 7:30
  • 1
    your mistake: tuple_of_tuples+=(x,). But underperformant like hell. Commented Mar 9, 2017 at 7:31
  • thanks for correcting my mistake. am even below underperformant! Commented Mar 9, 2017 at 8:28

1 Answer 1

5
In [1]: list1= [('2', '297'), ('6', '297')]

In [2]: tuple(list1)
Out[2]: (('2', '297'), ('6', '297'))
Sign up to request clarification or add additional context in comments.

4 Comments

tuple(list1) is not working for the below list, list1=[('2','297)] , tuple(list1) is giving the o/p as (('2', '297'),) , why it is coming like this? how to get the o/p as (('2','297)) ?
please help me on this
The comma is what makes the tuple, the parentheses are only needed for disambiguation. A single-element tuple is denoted by a comma. See docs.python.org/3/tutorial/…
Thanks for your help, am a beginner.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.