1

I have a list that contains other lists:

list_of_lists = [[['2019-03-27-16:08:21 Now:(87.0866) Epoch:(1553728101) 45(secs)ago ItemID:(51141)', '2019-03-20-16:09:21 7d:(87.2040) Epoch:(1553123361) 604785(secs)ago ItemID:(51141)', 'Interval:(1m) Diff:(-0.1174) Now_[less-than]_Past(7d) GPM:(0.00008153) +GROWTH'], 'OK: Date:[2021-04-07 10:43:10.037075] Days.Until:[741.773648417]']]

How do I merge these lists into one list so that it looks like this:

['2019-03-27-16:08:21 Now:(87.0866) Epoch:(1553728101) 45(secs)ago ItemID:(51141)', '2019-03-20-16:09:21 7d:(87.2040) Epoch:(1553123361) 604785(secs)ago ItemID:(51141)', 'Interval:(1m) Diff:(-0.1174) Now_[less-than]_Past(7d) GPM:(0.00008153) +GROWTH', 'OK: Date:[2021-04-07 10:43:10.037075] Days.Until:[741.773648417]']

I've found some code online but it doesn't do what I want:

flattened_list = [y for x in list_of_lists for y in x]

I also would prefer a solution that does not involve having to pip install any modules that isn't part of the default python package.

7
  • 2
    Possible duplicate of How to make a flat list out of list of lists? Commented Mar 27, 2019 at 23:18
  • @Bazingaa isn't that the same solution OP attempted and mentioned in the post that didn't work? Commented Mar 27, 2019 at 23:19
  • @davedwards: There are over 10 answers in the duplicate. Read and try them all Commented Mar 27, 2019 at 23:20
  • 1
    @davedwards: 53 answers to be specific Commented Mar 27, 2019 at 23:21
  • great, many options, thanks Commented Mar 27, 2019 at 23:21

2 Answers 2

1

With a simple recursive method you can handle any level of nesting:

def flat(l):
    if isinstance(l, list):
        result = []
        for i in l:
            result = result + flat(i)
        return result
    else:
        return [l]

>>> flat(list_of_lists)
['2019-03-27-16:08:21 Now:(87.0866) Epoch:(1553728101) 45(secs)ago ItemID:(51141)', '2019-03-20-16:09:21 7d:(87.2040) Epoch:(1553123361) 604785(secs)ago ItemID:(51141)', 'Interval:(1m) Diff:(-0.1174) Now_[less-than]_Past(7d) GPM:(0.00008153) +GROWTH', 'OK: Date:[2021-04-07 10:43:10.037075] Days.Until:[741.773648417]']

Another example:

>>> flat([1,2,[3,[4,5]],6,[7,8]])
[1, 2, 3, 4, 5, 6, 7, 8]
Sign up to request clarification or add additional context in comments.

Comments

0

Because some of the items in your list contains lists and others don't, the simple flatten options cannot be used directly. I would suggest creating a function that will flatten the list of lists recursively (including items that are lists themselves):

def flatten(aList):
    if not isinstance(aList,list): return [aList]
    return [ item for subList in aList for item in flatten(subList)] 

flattened_list = flatten(list_of_lists)

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.