468,533 Members | 1,909 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,533 developers. It's quick & easy.

Nested lists, simple though


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/Nested-lists%2...p16799674.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jun 27 '08 #1
7 2026
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]

Jun 27 '08 #2
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/Nested-lists%2...6799674p167996...
Sent from the Python - python-list mailing list archive at Nabble.com.
The first idea that comes to mind is reduce(lambda x, y: x + y,
list_of_lists, [])
Jun 27 '08 #3
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.
Jun 27 '08 #4
On 2008-04-20, Zethex <ze****@hotmail.comwrote:
Im a bit new to python. Anyway working on a little project of mine and i
have nested lists
What you want to do is usually called "flattening".

http://mail.python.org/pipermail/tut...ry/002914.html
http://aspn.activestate.com/ASPN/Coo.../Recipe/363051
http://aspn.activestate.com/ASPN/Coo.../Recipe/121294

--
Grant Edwards grante Yow! HAIR TONICS, please!!
at
visi.com
Jun 27 '08 #5
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.

Jun 27 '08 #6
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

Jun 27 '08 #7
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
Jun 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by naturalborncyborg | last post: by
15 posts views Thread by Xah Lee | last post: by
5 posts views Thread by John Topley | last post: by
4 posts views Thread by steven | last post: by
6 posts views Thread by Licheng Fang | last post: by
reply views Thread by TAL651 | last post: by
4 posts views Thread by RossRGK | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.