3

I want to convert my list of lists and within these lists there are list into only list of lists. For example:

My code:

a = [[], [], [['around_the_world']], [['around_the_globe']], [], [], [], []]
aaa = len(a)
aa = [[] for i in range(aaa)]
for i, x in enumerate(a):
    if len(x) != 0:
        for xx in x:
            for xxx in xx:
                aa[i].append(xxx)
print(aa)

Currently:

a = [[], [], [['around_the_world']], [['around_the_globe']], [], [], [], []]

to expected:

[[], [], ['around_the_world'], ['around_the_globe'], [], [], [], []]

My current code works in finding the expected output. However, i have to use too many for loop and its too deep. Is there a shorter way to do so like just in one or 2 lines?

1
  • Is there anyone can help me in this? Commented Oct 5, 2018 at 5:28

5 Answers 5

1

You can do it with list comprehension, just by checking to see whether the nested list is empty or not, and, if not, replacing the outer list with the inner list (by index).

data = [[], [], [['around_the_world']], [['around_the_globe']], [], [], [], []]

result = [d[0] if d else d for d in data]
print(result)

# OUTPUT
# [[], [], ['around_the_world'], ['around_the_globe'], [], [], [], []]
Sign up to request clarification or add additional context in comments.

1 Comment

True! I am learning from many answers. I am really glad with that
1

I used itertools for this . for more info flatten list of list in python

import itertools
a=[[], [], [['around_the_world']], [['around_the_globe']], [], [], [], []]
a = [list(itertools.chain(*li)) for li in a]
print(a)

Output

[[], [], ['around_the_world'], ['around_the_globe'], [], [], [], []]

8 Comments

Can I know what is the list() and * for?
@Beginner list() is converting something to list and * is to unpack something
It is for converting a generator object to a list , [<itertools.chain object at 0x7f471409b470> , ...... ] this is the output of itertools.chain . list() is used to convert this into the output you see above
* is essentially for unpacking eg if we want some variable amount of arguments into a function then we can put all the arguments into a list and the call functioncall(*thatlistname) and that will unpack the arguments into the function as arguments
@Beginner - google "splat" or "splatting" to learn more about the * operator.
|
1

Try next with iter:

a = [[], [], [['around_the_world']], [['around_the_globe']], [], [], [], []]
print([next(iter(i),[]) for i in a])

Output:

[[], [], ['around_the_world'], ['around_the_globe'], [], [], [], []]

Comments

1

All the short methods look to have been resolved here is a expanded explanation of what is happening in most these processes. Pretty much you are unpacking the nested list and not touching the empty lists.

a = [[], [], [['around_the_world']], [['around_the_globe']], [], [], [], []]
result = []

for i in a:
    if i == []:
        result.append(i)
    else:
        result.append(*i)

print(result)
# [[], [], ['around_the_world'], ['around_the_globe'], [], [], [], []]

1 Comment

@Beginner no prob :)
0

Since you want to flatten the inner sub-lists, you can use list comprehension within a list comprehension:

[[i for s in l for i in s] for l in a]

This returns:

[[], [], ['around_the_world'], ['around_the_globe'], [], [], [], []]

3 Comments

Glad to be of help. Do mark what you think is the best answer as accepted once you've reviewed them all. :-)
@blhsing Yeah should mark, but still have to way a few more minutes
Don't count @U9-Forward out yet he got some tricks up his sleeve!

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.