Nested lists, simple though

Z

Zethex

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
 
S

sturlamolden

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]
 
J

Jason Scheirer

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

The first idea that comes to mind is reduce(lambda x, y: x + y,
list_of_lists, [])
 
G

George Sakkis

Im a bit new to python.  Anyway working on a little project of mine and i
have nested lists

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
--
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.
 
S

sturlamolden

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.
 
S

sturlamolden

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
 
D

Diez B. Roggisch

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top