-1

How do I convert a list

    [1, 2, 3, 4, 5]

to list of tuple

    [(1, 2, 3, 4, 5)]

And convert a tuple

    (1, 2, 3, 4, 5)

to list of tuple

    [(1, 2, 3, 4, 5)]
san
  • 29
  • 6

4 Answers4

0

from list:

[tuple(x)]

from tuple:

[x]

i.e.

>>> x = [1,2,3]
>>> [tuple(x)]
[(1, 2, 3)]
>>> x = (1, 2, 3)
>>> [x]
[(1, 2, 3)]
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
0
arr = [1, 2, 3, 4]
print(arr)
tpl = (arr,)
print(type(tpl), tpl)

output:

[1, 2, 3, 4]
<class 'tuple'> ([1, 2, 3, 4],)

case 2:

tpl_2 = (1, 2, 3, 4)
print(tpl_2)
arr_2 = [tpl_2]
print(type(arr_2), arr_2)

output:

(1, 2, 3, 4)
<class 'list'> [(1, 2, 3, 4)]
Alex Ozerov
  • 988
  • 8
  • 21
0

What about doing the following:-

your_list = [1,2,3,4]
new_list = [tuple(your_list)]

And in the second case:-

your_tuple = (1,2,3,4)
new_list = [your_tuple]
0

try it !

l=[1, 2, 3, 4, 5]
t=tuple(i for i in l)
t

output:

(1, 2, 3, 4, 5)

and

tl = [t]
tl

output:

[(1, 2, 3, 4, 5)]
Willian Vieira
  • 646
  • 3
  • 9