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

Mazdak
  • 105,000
  • 18
  • 159
  • 188
Suba
  • 27
  • 1
  • 2

1 Answers1

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

In [2]: tuple(list1)
Out[2]: (('2', '297'), ('6', '297'))
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • 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)) ? – Suba Mar 09 '17 at 08:08
  • please help me on this – Suba Mar 09 '17 at 08:12
  • The comma is what makes the tuple, the parentheses are only needed for disambiguation. A single-element tuple is denoted by a comma. See https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences – Tim Pietzcker Mar 09 '17 at 08:13
  • Thanks for your help, am a beginner. – Suba Mar 09 '17 at 08:27