By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,291 Members | 3,096 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,291 IT Pros & Developers. It's quick & easy.

Nested lists, simple though

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

Jun 27 '08 #1
Share this Question
Share on Google+
7 Replies


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]

Jun 27 '08 #2

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/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

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

P: n/a
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

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.

Jun 27 '08 #6

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

Jun 27 '08 #7

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

This discussion thread is closed

Replies have been disabled for this discussion.