Reputation:
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
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
Reputation: 1604
You could use any of these:
list(zip(*L)[0])
[item for subitem in L for item in subitem]
list(itertools.chain(*L))
functools.reduce(lambda x, y: x + y, L)
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.
[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']]
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.
functools.reduce(lambda x, y: x + y, L)
This solution is just to show how else you can accomplish this.
Upvotes: 3
Reputation: 605
Numpy's squeeze removes extra dimensions from lists and tuples.
from numpy import squeeze
L = squeeze(L).tolist()
Upvotes: 1
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
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