user9374872
user9374872

Reputation:

How to convert list of lists in tuple to list of lists in python?

I have this as output:

L = ([['AAA', '193.0', 'GGG']], [['BBB', '196.33333333333334', 'TTT']], 
[['CCC', '18.666666666666668', 'AAA']])

I want it to be converted to a list of lists like:

L = [['AAA', '193.0', 'GGG'], ['BBB', '196.33333333333334', 'TTT'], 
['CCC', '18.666666666666668', 'AAA']]

I have tried to use

L = list(L)

and

[list(elem) for elem in L]

and

L = map(list, L)

But I cannot get any of them to work. Can someone please help me out?

Upvotes: 2

Views: 113

Answers (5)

sgDysregulation
sgDysregulation

Reputation: 4417

This could be done using map, typical use map(function, iterable)

In python 2.7 map returns a list of the result of applying the function to each item in the iterable. so we can use

L = map(lambda l:l[0],L)

In python 3.x map returns an iterator that applies function to every item of iterable(this is the tuple in our case), yielding the results, so list will be required to convert the resulting iterator to a list. i.e.

L = list(map(lambda l:l[0],L))

In either cases, the output of print(L) will be

[['AAA', '193.0', 'GGG'],
 ['BBB', '196.33333333333334', 'TTT'],
 ['CCC', '18.666666666666668', 'AAA']]

The function lambda l: l[0] is an anonymous function that takes a list and return the first element in it. in other words it squeezes the list elements of the tuple.

Upvotes: 1

Ilija
Ilija

Reputation: 1604

You could use any of these:

  1. list(zip(*L)[0])
  2. [item for subitem in L for item in subitem]
  3. list(itertools.chain(*L))
  4. functools.reduce(lambda x, y: x + y, L)

First solution

list(zip(*L)[0])

First solution converts resulting tuple in list, just like you wanted. But if result is just used for reading, you do not need a list. tuple will be just fine.


Second solution

[item for subitem in L for item in subitem]

Second case will flatten even this:

L = ([['AAA', '193.0', 'GGG'], ['AAA', '193.0', 'GGG']], [['BBB', '196.33333333333334', 'TTT']], [['CCC', '18.666666666666668', 'AAA']])

to

[['AAA', '193.0', 'GGG'],
 ['AAA', '193.0', 'GGG'],
 ['BBB', '196.33333333333334', 'TTT'],
 ['CCC', '18.666666666666668', 'AAA']]

Third solution

list(itertools.chain(*L))

Third solution is even prettier. What it does it goes through a list of iterables and returns each iterable's elements.

Fourth solution

functools.reduce(lambda x, y: x + y, L)

This solution is just to show how else you can accomplish this.

Upvotes: 3

Michael H.
Michael H.

Reputation: 605

Numpy's squeeze removes extra dimensions from lists and tuples.

   from numpy import squeeze
    L = squeeze(L).tolist()

Upvotes: 1

Jean-François Fabre
Jean-François Fabre

Reputation: 140196

you have a list of lists inside your tuple, but each list contains one element.

Rebuild the list using a list comprehension, taking the one and only element of the list:

L = ([['AAA', '193.0', 'GGG']], [['BBB', '196.33333333333334', 'TTT']],
[['CCC', '18.666666666666668', 'AAA']])

L = [x[0] for x in L]

print(L)

result:

[['AAA', '193.0', 'GGG'], ['BBB', '196.33333333333334', 'TTT'], ['CCC', '18.666666666666668', 'AAA']]

Upvotes: 2

Daniel Roseman
Daniel Roseman

Reputation: 599630

Your logic is flawed. You have a tuple of single-element lists containing lists, and you just want a list of lists. So you need to extract the first element of each list:

L = [elem[0] for elem in L]

Upvotes: 1

Related Questions