Coder 477
Coder 477

Reputation: 435

adding list of tuples to a new tuple in python

I have a list of tuples like the following

list=[(1,2),(3,4),(5,6)]

now I need to add them to a new tuple tup=()

my resultant tuple should look like tup=((1,2),(3,4),(5,6))

I tried using the following code:

 for each in list:

    tup=tup,each

the result is

(((), (627, 2)), (627, 3))

Can someone help me solving this?

Upvotes: 1

Views: 54

Answers (1)

Kasravnd
Kasravnd

Reputation: 107287

You just need to convert with tuple function :

>>> my_list=[(1,2),(3,4),(5,6)]
>>> tuple(my_list)
((1, 2), (3, 4), (5, 6))

note : dont use the built-in functions name or python keywords as your structure and variables name!

Upvotes: 6

Related Questions