0

Hey I am having trouble trying to make a list of list into just one please help:

I want:

a = [['a'], 'b', 'c', 'd', 'e']

To become:

a = ['a', 'b', 'c', 'd', 'e']

Thanks

Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56
Kevin
  • 47
  • 5
  • 2
    usually something like this implies some other odd behavior elsewhere in your code. Are you sure it's not a better idea to change what generates a list of maybe lists and maybe strings? – Adam Smith May 19 '17 at 04:17
  • 1
    @arboreal84 not quite, but close. That talks about flattening a list of lists. This talks about flattening a list of things that might be lists or might be values. – Adam Smith May 19 '17 at 04:19
  • Good observation @AdamSmith – arboreal84 May 19 '17 at 04:20
  • 2
    @ChihebNexus ditto to my comment above. – Adam Smith May 19 '17 at 04:23
  • @AdamSmith Yes. But still a possibile duplicate. There is a lot of answers there. No reason to copy paste them here. Or maybe someone has a new approach that is not listed in the other post?. – Chiheb Nexus May 19 '17 at 04:26
  • The best solution is just "Hey you should stay away from heterogeneous lists," but that's a bit of a cop out isn't it? If we could solve every programming question by berating those that came before us, we'd....wait a second, in my experience that's *exactly* what we do. – Adam Smith May 19 '17 at 04:28
  • @ChihebNexus no, this is a different question completely. Answers that work in the linked question's case don't work here except by the same accident that led to a couple of the early answers work here (`itertools.chain.from_iterable` for example) – Adam Smith May 19 '17 at 04:30
  • This is most likely an XY question, which is why my first comment to the submitter was "Are you sure you should be ending up with this output to parse in the first place?" – Adam Smith May 19 '17 at 04:33
  • @AdamSmith, yes you're right. But, a programmer/developer should'nt try at least to run and debug some solutions ? then look at the documentations ? then Stackoverflow ? Then Google, maybe ? Then asking for some help. i don't know but for me such a questions should be closed. you have more reputation than me, tell me what do you think please. – Chiheb Nexus May 19 '17 at 04:34

2 Answers2

4

As long as your lists can't be nested more than one deep, you could do:

def flatten(lst):
    for el in lst:
        if isinstance(el, list):
            yield from el
        else:
            yield el

Then call list on the result if you actually need it as a list (usually an iterator will do).

a = [['a'], 'b', 'c', 'd', 'e']
flat_a = flatten(a)  # not a list, but an iterator that returns flat values
flat_a_as_lst = list(flat_a)  # actually a list
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • How about `[{1,2},3,4] ` and `[(1,2),3,4]` ? This question still a duplicate no ?. Otherwise your answer is correct only for your current input. – Chiheb Nexus May 19 '17 at 04:30
  • 2
    @ChihebNexus not as this question is asked and not as the example data shows, which specifically talks about lists within lists (not sets or tuples, which have different meanings anyway). – Adam Smith May 19 '17 at 04:33
  • 1
    You answer is correct and efficient. I'm only trying to figure out if such a question can be marked as duplicate or not. Otherwise, +1 for using a generator. – Chiheb Nexus May 19 '17 at 04:39
  • I think, we can make use of iterable! `import itertools` `list_of_lists = [[1],2]` `flattened_list = list(itertools.chain(*list_of_lists))` NOTE: But this will not work on int objects as they are not iterable! – abhinav May 19 '17 at 04:47
  • @abhinav which, unfortunately, is why that solution doesn't work (there's a now-deleted answer that also uses `itertools.chain`. Note that `itertools.chain.from_iterable(foo)` is equivalent to `itertools.chain(*foo)`. – Adam Smith May 19 '17 at 04:59
  • This is elegant; it is notwithstanding a bit funny coming from functional languages, that every python developer has to write this function on their own :) – matanster Jul 14 '18 at 10:33
  • @matanster You can generalize to an arbitrarily-deep data structure too by `yield from flatten(el)`. Though if you find it funny to have to write this in Python, you should never check out Go! – Adam Smith Jul 14 '18 at 15:04
1

Try to iterate all sub-lists of the list. For your list a:

a = [['a'], 'b', 'c', 'd', 'e']
flat_a = [subitem for item in a for subitem in (item if isinstance(item, list) else [item])]

Edit: considered Adams's comment.

grovina
  • 2,999
  • 19
  • 25