2

I have the following nested list of lists:

a = [[[[-79.43402638260521, -1.69184588855758], [-79.4339722432865, -1.691845844583909], [-79.43397178076256, -1.691851284533779],
    [-79.43395283944169, -1.692053292637794], [-79.43395281911414, -1.692054736321033], [-79.43395535750368, -1.692093535418117],
    [-79.43390444734398, -1.69223087834723], [-79.43390428016939, -1.692231372437897], [-79.43374523144152, -1.692750043925838],
    [-79.4340256570161, -1.692750271834557], [-79.43402638260521, -1.69184588855758]]], [[[-79.43381375958064, -1.691845715849684],
    [-79.43312765678151, -1.691845158387183], [-79.4331269307764, -1.692749541273626], [-79.43354270912953, -1.692749879305633],
    [-79.43364983051107, -1.692588468489809], [-79.4336510738479, -1.692585646334773], [-79.43371548446397, -1.692327269168548],
    [-79.43380554258165, -1.692094789340216], [-79.43380615195998, -1.692091785860122], [-79.43381375958064, -1.691845715849684]]]]

How would I go from that to:

a = [[-79.43402638260521, -1.69184588855758], [-79.4339722432865, -1.691845844583909], [-79.43397178076256, -1.691851284533779], 
[-79.43395283944169, -1.692053292637794], [-79.43395281911414, -1.692054736321033], [-79.43395535750368, -1.692093535418117], 
[-79.43390444734398, -1.69223087834723], [-79.43390428016939, -1.692231372437897], [-79.43374523144152, -1.692750043925838], 
[-79.4340256570161, -1.692750271834557], [-79.43402638260521, -1.69184588855758], [-79.43381375958064, -1.691845715849684], 
[-79.43312765678151, -1.691845158387183], [-79.4331269307764, -1.692749541273626], [-79.43354270912953, -1.692749879305633], 
[-79.43364983051107, -1.692588468489809], [-79.4336510738479, -1.692585646334773], [-79.43371548446397, -1.692327269168548], 
[-79.43380554258165, -1.692094789340216], [-79.43380615195998, -1.692091785860122], [-79.43381375958064, -1.691845715849684]]

so basically remove the nested list and the double brackets at the beginning and end of each list. I've tried the following and ittertools without succes:

flatten = lambda list: [item for sublist in list for item in sublist]

Note: len(a) == 2

Many thanks!

4
  • 3
    In you particular case, a[0][0]? Commented Feb 23, 2017 at 16:39
  • 3
    @CarlesMitjans, @Arman, len(a) == 2 Commented Feb 23, 2017 at 16:40
  • 1
    @falsetru you are right, didn't notice Commented Feb 23, 2017 at 16:40
  • 1
    @falsetru, exactly, that is why a[0][0] does not work in this case Commented Feb 23, 2017 at 16:41

5 Answers 5

2

This does the job.

a = a[0][0] + a[1][0]

This can be extended as,

a = sum([a[i][0] for i in range(len(a))], [])
Sign up to request clarification or add additional context in comments.

9 Comments

This seems to me to be the most sensible solution. It can also be adapted to work in case there are more than two elements easily.
exactly! if the format is consistent, this is the easiest way. @sphericalcowboy
can you guys comment the reasons for downvoting? this is not a "wrong" answer.
Yes. In the case of lists, it worked but it is not the right way. :) I'll edit it.
Thanks for the answer. This indeed seems the most logic solution as it is not depended on other libraries.
|
2

Using itertools.chain.from_iterable:

list(itertools.chain.from_iterable(xs[0] for xs in a))

Above is similar to: a[0][0] + a[1][0] + ... (without concatenation which cause list creation inbetween)


>>> a = [[[[-79.43402638260521, -1.69184588855758],
...        [-79.4339722432865, -1.691845844583909],
...        [-79.43397178076256, -1.691851284533779],
...        [-79.43395283944169, -1.692053292637794],
...        [-79.43395281911414, -1.692054736321033],
...        [-79.43395535750368, -1.692093535418117],
...        [-79.43390444734398, -1.69223087834723],
...        [-79.43390428016939, -1.692231372437897],
...        [-79.43374523144152, -1.692750043925838],
...        [-79.4340256570161, -1.692750271834557],
...        [-79.43402638260521, -1.69184588855758]]],
...      [[[-79.43381375958064, -1.691845715849684],
...        [-79.43312765678151, -1.691845158387183],
...        [-79.4331269307764, -1.692749541273626],
...        [-79.43354270912953, -1.692749879305633],
...        [-79.43364983051107, -1.692588468489809],
...        [-79.4336510738479, -1.692585646334773],
...        [-79.43371548446397, -1.692327269168548],
...        [-79.43380554258165, -1.692094789340216],
...        [-79.43380615195998, -1.692091785860122],
...        [-79.43381375958064, -1.691845715849684]]]]
>>> 
>>> import itertools
>>> import pprint
>>> b = list(itertools.chain.from_iterable(xs[0] for xs in a))
>>> pprint.pprint(b)

result:

[[-79.43402638260521, -1.69184588855758],
 [-79.4339722432865, -1.691845844583909],
 [-79.43397178076256, -1.691851284533779],
 [-79.43395283944169, -1.692053292637794],
 [-79.43395281911414, -1.692054736321033],
 [-79.43395535750368, -1.692093535418117],
 [-79.43390444734398, -1.69223087834723],
 [-79.43390428016939, -1.692231372437897],
 [-79.43374523144152, -1.692750043925838],
 [-79.4340256570161, -1.692750271834557],
 [-79.43402638260521, -1.69184588855758],
 [-79.43381375958064, -1.691845715849684],
 [-79.43312765678151, -1.691845158387183],
 [-79.4331269307764, -1.692749541273626],
 [-79.43354270912953, -1.692749879305633],
 [-79.43364983051107, -1.692588468489809],
 [-79.4336510738479, -1.692585646334773],
 [-79.43371548446397, -1.692327269168548],
 [-79.43380554258165, -1.692094789340216],
 [-79.43380615195998, -1.692091785860122],
 [-79.43381375958064, -1.691845715849684]]

Comments

2

Assuming that your lists aren't arbitrarily nested but you're just trying to go one level deeper, you could opt for something very simple like

a = [item for sublist in a for subsublist in sublist for item in subsublist]

Demo

>>> a = [[[[-79.43402638260521, -1.69184588855758], [-79.4339722432865, -1.691845844583909], [-79.43397178076256, -1.691851284533779],
[-79.43395283944169, -1.692053292637794], [-79.43395281911414, -1.692054736321033], [-79.43395535750368, -1.692093535418117],
[-79.43390444734398, -1.69223087834723], [-79.43390428016939, -1.692231372437897], [-79.43374523144152, -1.692750043925838],
[-79.4340256570161, -1.692750271834557], [-79.43402638260521, -1.69184588855758]]], [[[-79.43381375958064, -1.691845715849684],
[-79.43312765678151, -1.691845158387183], [-79.4331269307764, -1.692749541273626], [-79.43354270912953, -1.692749879305633],
[-79.43364983051107, -1.692588468489809], [-79.4336510738479, -1.692585646334773], [-79.43371548446397, -1.692327269168548],
[-79.43380554258165, -1.692094789340216], [-79.43380615195998, -1.692091785860122], [-79.43381375958064, -1.691845715849684]]]]
>>> desired = [[-79.43402638260521, -1.69184588855758], [-79.4339722432865, -1.691845844583909], [-79.43397178076256, -1.691851284533779], 
[-79.43395283944169, -1.692053292637794], [-79.43395281911414, -1.692054736321033], [-79.43395535750368, -1.692093535418117], 
[-79.43390444734398, -1.69223087834723], [-79.43390428016939, -1.692231372437897], [-79.43374523144152, -1.692750043925838], 
[-79.4340256570161, -1.692750271834557], [-79.43402638260521, -1.69184588855758], [-79.43381375958064, -1.691845715849684], 
[-79.43312765678151, -1.691845158387183], [-79.4331269307764, -1.692749541273626], [-79.43354270912953, -1.692749879305633], 
[-79.43364983051107, -1.692588468489809], [-79.4336510738479, -1.692585646334773], [-79.43371548446397, -1.692327269168548], 
[-79.43380554258165, -1.692094789340216], [-79.43380615195998, -1.692091785860122], [-79.43381375958064, -1.691845715849684]]

>>> a = [item for sublist in a for subsublist in sublist for item in subsublist]
>>> a == desired
True

However as noted by falsetru you've got an empty level of nesting in your example, in which case the solution can be cleaned up nicely as they have shown.

Comments

0

You can try:

[z for x in a for y in x for z in y]

And to prove it:

len([y for x in a for y in x]) == 21

You can see it properly using pprint:

pprint.pprint(a)

Ouput:

[[[[-79.43402638260521, -1.69184588855758],
   [-79.4339722432865, -1.691845844583909],
   [-79.43397178076256, -1.691851284533779],
   [-79.43395283944169, -1.692053292637794],
   [-79.43395281911414, -1.692054736321033],
   [-79.43395535750368, -1.692093535418117],
   [-79.43390444734398, -1.69223087834723],
   [-79.43390428016939, -1.692231372437897],
   [-79.43374523144152, -1.692750043925838],
   [-79.4340256570161, -1.692750271834557],
   [-79.43402638260521, -1.69184588855758]]],
 [[[-79.43381375958064, -1.691845715849684],
   [-79.43312765678151, -1.691845158387183],
   [-79.4331269307764, -1.692749541273626],
   [-79.43354270912953, -1.692749879305633],
   [-79.43364983051107, -1.692588468489809],
   [-79.4336510738479, -1.692585646334773],
   [-79.43371548446397, -1.692327269168548],
   [-79.43380554258165, -1.692094789340216],
   [-79.43380615195998, -1.692091785860122],
   [-79.43381375958064, -1.691845715849684]]]]

pprint.pprint([z for x in a for y in x for z in y])

Output:

[[-79.43402638260521, -1.69184588855758],
 [-79.4339722432865, -1.691845844583909],
 [-79.43397178076256, -1.691851284533779],
 [-79.43395283944169, -1.692053292637794],
 [-79.43395281911414, -1.692054736321033],
 [-79.43395535750368, -1.692093535418117],
 [-79.43390444734398, -1.69223087834723],
 [-79.43390428016939, -1.692231372437897],
 [-79.43374523144152, -1.692750043925838],
 [-79.4340256570161, -1.692750271834557],
 [-79.43402638260521, -1.69184588855758],
 [-79.43381375958064, -1.691845715849684],
 [-79.43312765678151, -1.691845158387183],
 [-79.4331269307764, -1.692749541273626],
 [-79.43354270912953, -1.692749879305633],
 [-79.43364983051107, -1.692588468489809],
 [-79.4336510738479, -1.692585646334773],
 [-79.43371548446397, -1.692327269168548],
 [-79.43380554258165, -1.692094789340216],
 [-79.43380615195998, -1.692091785860122],
 [-79.43381375958064, -1.691845715849684]]

Comments

0

You need to iterate over the nested lists and join them.

A simple way:

def flatten1(list_of_lists):
  "Flattens one level of lists."
  result = []
  for sub_list in list_of_lists:
    result.extend(sub_list)
  return result

A clever way, exploiting the fact that you can add lists:

flatten1 = lambda(list_of_lists): sum(list_of_lists, [])

Now, you can a[0] = flatten1(a[0]).

Comments

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.