0

Help me please with this problem: I have a list containing numbers and lists kind of:

list = [1,2,[3,4,5],6,7,[8,9]]

What I want to get is a list of just numbers (mergin included lists in parent list): Example:

new_list = [1,2,3,4,5,6,7,8,9]
c00p3r.web
  • 808
  • 3
  • 10
  • 21
  • 1
    @msw: Look at OP's title. It's hard to search for things when you don't know the right words. – Blender Jul 05 '13 at 08:07
  • I saw big signature saying "marked as duplicate" what is the point of repeating that in comments? I just didn't know that it's called 'flatten'. Not knowing is not a crime. Everyone makes mistakes – c00p3r.web Jul 08 '13 at 12:16

2 Answers2

3

Using recursion and collections.Iterable:

>>> from collections import Iterable
>>> def flat(lst):
...     for parent in lst:
...         if not isinstance(parent, Iterable):
...             yield parent
...         else:
...             for child in flat(parent):
...                 yield child
...
>>> list(flat(t))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Just remember to not name lists after their own type :).

c00p3r.web
  • 808
  • 3
  • 10
  • 21
TerryA
  • 58,805
  • 11
  • 114
  • 143
2

I personally use this method :

This is a functional version of recursive flatten which handles both tuples and lists, and lets you throw in any mix of positional arguments. Returns a generator which produces the entire sequence in order, arg by arg:

flatten = lambda *n: (e for a in n
    for e in (flatten(*a) if isinstance(a, (tuple, list)) else (a,)))

Usage:

l1 = ['a', ['b', ('c', 'd')]]
l2 = [0, 1, (2, 3), [[4, 5, (6, 7, (8,), [9]), 10]], (11,)]
print list(flatten(l1, -2, -1, l2))
['a', 'b', 'c', 'd', -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

This works fine with both python 3.x and python 2.x

Here is a regex solution for the same ..

 import re

 def Flatten(TheList):
   a = str(TheList)
   b,crap = re.subn(r'[\[,\]]', ' ', a)
   c = b.split()
   d = [int(x) for x in c]

   return(d)
tusharmakkar08
  • 706
  • 1
  • 12
  • 32
  • 1
    -1 Couldn't you just have a regular function? Why stuck a long line into a lambda and name it to a variable right after it. That is not the purpose of the `lambda` keyword is what I'm saying. 'anonymous function' – alexpinho98 Jul 05 '13 at 08:09
  • -1. What if `L = [1, {2, 3}, 4, (5, 6, [7, 8], 9), {10, 11, 12, 13}]`? – TerryA Jul 05 '13 at 08:14
  • @Haidro : The question demanded only for square brackets though.. – tusharmakkar08 Jul 05 '13 at 08:53
  • @alexpinho98 : I think using this makes it neater and according to zen of python .. `Beautiful is better than ugly.` Anyways to make it clearer about the use of `lambda` you should better see [this](http://stackoverflow.com/questions/890128/python-lambda-why).Yes, regex is no doubt a better opinion ... Anyways edited the answer – tusharmakkar08 Jul 05 '13 at 08:57
  • 2
    @TusharMakkar IMHO I think the accepted answer's code is much more readable, pretty and easier to explain than the `lambda` version. `lambda` has it's uses and I've used it for many things like very short 'algebraic' functions and for functions like ´map´ and ´filter´, but i think that in this case ´lambda´ is not the right way. A good rule of thumb might be: 'if the statement is longer than some arbitrary amount(say 80) of characters and it involves recursion: use a normal function, else: use ´lambda´. – alexpinho98 Jul 05 '13 at 11:40
  • @alexpinho98 completely agree with you. Wanted to make a comment but you read my mind :) – c00p3r.web Jul 05 '13 at 11:48