Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python lists append return value

Tags:

python

list

I wanted to create a simple binary tree as followed by this image:

http://imgur.com/QCVSW.png

basically empty , but the last values so I created the root list :

root = [list(),list()]

and made a recursive function to populate it all :

def TF(nodeT,nodeF , i):
    if i == 35 : return 'done'

    TF(nodeT.append([]),nodeT.append([]) , i = i + 1) #append T , F in the true node
    TF(nodeF.append([]),nodeT.append([]) , i = i + 1) #append T , F in the false node

my problem is simple list.append(something) in python return "None" so as soon as the function get called again (TF(None,None,1)) None.append doesnt exists.

how do I solve this? thanks in advance.

also if you have any suggestion on how to make this more efficient or in another way (never got to test my code so I am not sure on how it will do)

(my final goal is to have a True False map and an argument so : "FTFTFFFTFTF" will bring up the letter "M" etc ...)

like image 406
Lior Co Avatar asked Dec 16 '09 22:12

Lior Co


People also ask

Does append return anything Python?

append method in Python is to insert an item at the end of the list, but it doesn't return anything but None . In your code: y = x.

Does append return a list?

append() is a method which performs an action toward new_lst and doesn't return anything. I think you want to . append() the list after the powers in powers loop (where it is at now) but not return it.

How do you append an output to a list in Python?

There are four methods to add elements to a List in Python. append() : append the element to the end of the list. insert() : inserts the element before the given index. extend() : extends the list by appending elements from the iterable.

What happens if you append a list to a list Python?

append() adds a list inside of a list. Lists are objects, and when you use . append() to add another list into a list, the new items will be added as a single object (item).


1 Answers

In python you can use the "+" operator to contatenate two lists leaving the originals untouched. I guess that's what you want to do according to your question title. Thus

[1, 2] + [3] 

will return

[1, 2, 3]

so you can use it more in a "functional fashion". Just in case you need it

[1, 2].__add__([3])

is the equivalent to the expression before.

like image 126
csierra Avatar answered Sep 29 '22 18:09

csierra