Home > Back-end >  Convert list of tuples to list Python
Convert list of tuples to list Python

Time:11-14

How do I convert

[('Paulo Coelho', 74, 'brasileño'), ('Ziraldo', 89, 'brasileño')]

to

[['Paulo Coelho', 74, 'brasileño'], ['Ziraldo', 89, 'brasileño']]

any method for different sizes of tuples?

CodePudding user response:

All you need to do is loop through the list and convert every element from a tuple to a list using list()

for i in range(len(lis)):
    lis[i] = list(lis[i])

CodePudding user response:

You can use list() and list comprehension.

>>> l = [('Paulo Coelho', 74, 'brasileño'), ('Ziraldo', 89, 'brasileño')]
>>> c = [list(t) for t in l]
>>> c
[['Paulo Coelho', 74, 'brasileño'], ['Ziraldo', 89, 'brasileño']]

CodePudding user response:

list() function will convert tuple into list. To convert all tuples in array you have to traverse.

  arr = [('Paulo Coelho', 74, 'brasileño'), ('Ziraldo', 89, 'brasileño')]
        for i in range(len(arr)):
            arr[i] = list(arr[i])
  • Related