0

How can I transform

list = [[68.0], [79.0], [6.0]],  ... [[176.0], [120.0], [182.0]]

into

result = [68.0, 79.0, 6.0, 8.0], ... [176.0, 120.0, 182.0]
4
  • What should the result look like? Commented May 21, 2013 at 15:01
  • for example, I need individual lists from variable 'list' like this: list1 = [68.0, 79.0, 6.0, 8.0, 61.0, 88.0, 59.0, 91.0] Commented May 21, 2013 at 15:03
  • @towi, I would like to point out that original question was not only about to flatten out sublists but also to map them as well so that individual lists can be indexed out as required. Commented May 22, 2013 at 14:02
  • @Ibe Oh? I could not see that from your original formulation. I am sorry -- but feel free to correct my correction. I can not see what you are driving at. Commented May 28, 2013 at 20:01

5 Answers 5

1

If I've correctly understood what input_lists should actually look like, then I think what you're after is creating a dict so that dict[n] is your nth list. eg: the following code:

input_lists = [[[68.0], [79.0], [6.0], [8.0], [61.0], [88.0], [59.0], [91.0]],
       [[10.0], [11.0], [9.0], [120.0], [92.0], [12.0], [8.0], [13.0]],
       [[17.0], [18.0], [13.0], [14.0], [12.0], [176.0], [120.0], [182.0]]]


lists = {i:[el[0] for el in v] for i, v in enumerate(input_lists, start=1)}
# {1: [68.0, 79.0, 6.0, 8.0, 61.0, 88.0, 59.0, 91.0], 2: [10.0, 11.0, 9.0, 120.0, 92.0, 12.0, 8.0, 13.0], 3: [17.0, 18.0, 13.0, 14.0, 12.0, 176.0, 120.0, 182.0]}
Sign up to request clarification or add additional context in comments.

2 Comments

It gave error: TypeError: 'float' object is not subscriptable. sublists are not separated by commas and no outer brackets.
I first append the variable to a list within a for loop and then used your lists argument outside loop to separate lists. It worked. Thanks.
1
z = []
for x in list:
    for i in x:
        z.append(i)

2 Comments

z = [i for x in list for i in x] would be simper and more concise for that.
I wrote this answer taking in consideration that person who asked the question might be a beginner. This would be easy to read for him.
1

Am I missing something or a simple list comprehension would do? And also list is a keyword in python PLEASE DO NOT name your variables which create conflict with python keywords. It will bite you in places you cant imagine.

>>> mylist = [[68.0], [79.0], [6.0], [8.0], [61.0], [88.0], [59.0], [91.0]]
>>> [i[0] for i in mylist]
[68.0, 79.0, 6.0, 8.0, 61.0, 88.0, 59.0, 91.0] #this can be assigned to a new list var mylist1

UPDATE: Based on what lbe said, changing the approach -

>>> mylist =[[[68.0], [79.0], [6.0], [8.0], [61.0], [88.0], [59.0], [91.0]],
...        [[10.0], [11.0], [9.0], [120.0], [92.0], [12.0], [8.0], [13.0]],
...        [[17.0], [18.0], [13.0], [14.0], [12.0], [176.0], [120.0], [182.0]]]
>>> [i[0] for i in reduce(lambda x, y: x+y, mylist)]
[68.0, 79.0, 6.0, 8.0, 61.0, 88.0, 59.0, 91.0, 10.0, 11.0, 9.0, 120.0, 92.0, 12.0, 8.0, 13.0, 17.0, 18.0, 13.0, 14.0, 12.0, 176.0, 120.0, 182.0]

2 Comments

The only problem is that this list is part of a for loop so if I try your suggestion then mylist[0] returns: 68.0 10.0 17.0 not a horizontal list as I provided above .
@lbe: so you simply replace mylist by mylist[0] in the above code
0

First, the code above is invalid syntax. You need surrounding brackets or something similar.

Possibility one:

list =[[[68.0], [79.0], [6.0], [8.0], [61.0], [88.0], [59.0], [91.0]],
       [[10.0], [11.0], [9.0], [120.0], [92.0], [12.0], [8.0], [13.0]],
       [[17.0], [18.0], [13.0], [14.0], [12.0], [176.0], [120.0], [182.0]]]

Then list[0], list[1], etc. refer to the sub-lists.

Possibility two:

list =[[68.0], [79.0], [6.0], [8.0], [61.0], [88.0], [59.0], [91.0],
       [10.0], [11.0], [9.0], [120.0], [92.0], [12.0], [8.0], [13.0],
       [17.0], [18.0], [13.0], [14.0], [12.0], [176.0], [120.0], [182.0]]

That is, you want chunks of a long list. Then, just use slice syntax, ie, list[0:2] would be [[68.0], [79.0], [6.0]].

Comments

0

Assuming your data looks like it does in @JonClements answer, here's another approach using list comprehension.

[reduce(lambda x, y: x + y, l) for l in input_lists]
# [[68.0, 79.0, 6.0, 8.0, 61.0, 88.0, 59.0, 91.0],
#  [10.0, 11.0, 9.0, 120.0, 92.0, 12.0, 8.0, 13.0],
#  [17.0, 18.0, 13.0, 14.0, 12.0, 176.0, 120.0, 182.0]]

1 Comment

The only problem is that this list is part of a for loop so if I try your suggestion then input_lists[0] returns (not a horizontal list as I provided above): 68.0 10.0 17.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.