P: n/a

Im a bit new to python. Anyway working on a little project of mine and i
have nested lists
ie
Answer = [['computer', 'radeon', 'nvidia'], ['motherboard', 'asus']]
and so forth..,
Anyway the amount of [[]] do increase over time. Im just wondering is there
a simple way to add these together so they become 1 simple list, so it would
be ['computer'....'asus'] etc without the nested list. Its random the
amount each time so i cant just go a[0]+a[1].
Thank you if you can help

View this message in context: http://www.nabble.com/Nestedlists%2...p16799674.html
Sent from the Python  pythonlist mailing list archive at Nabble.com.  
Share this Question
P: n/a

On Apr 21, 12:25 am, Zethex <zet...@hotmail.comwrote:
Anyway the amount of [[]] do increase over time. Im just wondering is there
a simple way to add these together so they become 1 simple list, so it would
be ['computer'....'asus'] etc without the nested list. Its random the
amount each time so i cant just go a[0]+a[1].
Thank you if you can help
Does this answer your question?
>>a = [1,2,3] a.extend([4,5,6]) a
[1, 2, 3, 4, 5, 6]  
P: n/a

On Apr 20, 3:25*pm, Zethex <zet...@hotmail.comwrote:
Im a bit new to python. *Anyway working on a little project of mine and i
have nested lists
ie
Answer = [['computer', 'radeon', 'nvidia'], ['motherboard', 'asus']]
and so forth..,
Anyway the amount of [[]] do increase over time. *Im just wondering is there
a simple way to add these together so they become 1 simple list, so it would
be ['computer'....'asus'] etc without the nested list. *Its random the
amount each time so i cant just go a[0]+a[1].
Thank you if you can help

View this message in context:http://www.nabble.com/Nestedlists%2...6799674p167996...
Sent from the Python  pythonlist mailing list archive at Nabble.com.
The first idea that comes to mind is reduce(lambda x, y: x + y,
list_of_lists, [])  
P: n/a

On Apr 20, 6:50*pm, Jason Scheirer <jason.schei...@gmail.comwrote:
On Apr 20, 3:25*pm, Zethex <zet...@hotmail.comwrote:
Im a bit new to python. *Anyway working on a little project of mine and i
have nested lists
ie
Answer = [['computer', 'radeon', 'nvidia'], ['motherboard', 'asus']]
and so forth..,
Anyway the amount of [[]] do increase over time. *Im just wondering isthere
a simple way to add these together so they become 1 simple list, so it would
be ['computer'....'asus'] etc without the nested list. *Its random the
amount each time so i cant just go a[0]+a[1].
Thank you if you can help

The first idea that comes to mind is reduce(lambda x, y: x + y,
list_of_lists, [])
s/first/worst/
There, I fixed it for you.  
P: n/a

On Apr 21, 12:25 am, Zethex <zet...@hotmail.comwrote:
Anyway the amount of [[]] do increase over time.
You can flatten a nested list using a closure and recursion:
def flatten(lst):
tmp = []
def _flatten(lst):
for elem in lst:
if type(elem) != list:
tmp.append(elem)
else:
_flatten(elem)
_flatten(lst)
return tmp
However, CPython does not recurse very fast, but Cython and Pyrex do.
First, get rid of the closure, it is not supported in Cython or Pyrex
(at least not yet). Then change the recursive function to a cdef, i.e.
a normal C function which is called without the overhead of Python's
attribute lookup:
cdef _flatten(lst, tmp):
for elem in lst:
if type(elem) != list:
tmp.append(elem)
else:
_flatten(elem, tmp)
def flatten(lst):
tmp = []
_flatten(lst, tmp)
return tmp
Compile this with Cython or Pyrex, and you get a very fast nested list
flattener.
This also shows how easy it is to boost the performance of Python code
using Cython.  
P: n/a

On Apr 21, 2:35 am, sturlamolden <sturlamol...@yahoo.nowrote:
This also shows how easy it is to boost the performance of Python code
using Cython.
We can improve this further by getting rid of the tmp.append attribue
lookup:
cdef _flatten(lst, append):
for elem in lst:
if type(elem) != list:
append(elem)
else:
_flatten(elem, append)
def flatten(lst):
tmp = []
_flatten(lst, tmp.append)
return tmp  
P: n/a

The first idea that comes to mind is reduce(lambda x, y: x + y,
list_of_lists, [])
Which is not helping for arbitrary nested lists, as the OP wanted.
Diez   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 1910
 replies: 7
 date asked: Jun 27 '08
