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?

benvc
  • 14,448
  • 4
  • 33
  • 54
Beginner
  • 147
  • 8

5 Answers5

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'], [], [], [], []]
benvc
  • 14,448
  • 4
  • 33
  • 54
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'], [], [], [], []]
Albin Paul
  • 3,330
  • 2
  • 14
  • 30
  • Can I know what is the list() and * for? – Beginner Oct 05 '18 at 05:33
  • @Beginner `list()` is converting something to `list` and `*` is to unpack something – U13-Forward Oct 05 '18 at 05:35
  • It is for converting a generator object to a list , `[ , ...... ] ` this is the output of `itertools.chain` . `list()` is used to convert this into the output you see above – Albin Paul Oct 05 '18 at 05:37
  • what about the *? I am really curious. Please do share with me. – Beginner Oct 05 '18 at 05:41
  • I mean the * <--- this asterisk right next to "li" in chain(*li) – Beginner Oct 05 '18 at 05:43
  • 1
    * 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 – Albin Paul Oct 05 '18 at 05:44
  • In this case the function is `itertools.chain` and the variable no of arguments are provided by unpacking the elements in the list `li` – Albin Paul Oct 05 '18 at 05:45
  • 1
    @Beginner - google "splat" or "splatting" to learn more about the `*` operator. – benvc Oct 05 '18 at 05:50
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'], [], [], [], []]
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
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'], [], [], [], []]
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20
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'], [], [], [], []]
blhsing
  • 91,368
  • 6
  • 71
  • 106