8
votes

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

7
Would you elaborate on the use of this? - Hamish Grubijan
Please do not transliterate code from other languages into Python. Please try to think about how Python actually works; please think about how the algorithm actually works. Then -- after understanding -- write the Python. Simply replace C-language tokens with Python tokens will rarely give you working Python. - S.Lott
This is one of the things I detest about python, and IMO, is completely unpythonic. Any method that mutates self should return self. Any function that mutates anything should return that thing. - DylanYoung
I've upvoted DylanYoung, but I'd rephrase as follows: Having methods that mutate self returning self allows chaining and assignation in a more semantic and readable way, contributing to the language's expressive power. It's something I'd definitely would like to see in python! - nandilugio
python bad design strikes again - WestCoastProjects

7 Answers

27
votes

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.

19
votes

To solve your exact question, you can do this:

def list_append(lst, item):
  lst.append(item)
  return lst

and then list_append(lst, item) will append item to the lst and then return the lst.

4
votes

Dont append to the list, create them. Python has custom data structures btw :P

class BinTree(object):
    def __init__(self, left=None, right=None):
        self.left = left
        self.right = right
    def __str__(self):
        return " (%s, %s) " % (self.left, self.right)

def maketree( depth ):
    if depth == 0:
        return BinTree( True, False )
    return BinTree(
        maketree( depth-1 ),
        maketree( depth-1 ))

print maketree( 4 )

If you really, really want lists then replace BinTree( x, y ) with [x,y]

3
votes

You can append first and then pass the reference:

nodeT.append([])
TF(nodeT, nodeT, i + 1)

But your function makes no sense to me. Even if it did make sense, it would cause 2**35 function calls to TF, which would take rather a long time to complete.

1
votes

There's a better way to create a binary tree, but I could not understand what you want to do with it.

Here is the code:

>>> def tree(i):
    if i == 0:
        return ['T', 'F']
    return [tree(i-1), tree(i-1)]

>>> tree(3)
[[[['T', 'F'], ['T', 'F']], [['T', 'F'], ['T', 'F']]], [[['T', 'F'], ['T', 'F']], [['T', 'F'], ['T', 'F']]]]
1
votes

Adding to @csierra 's answer - which I upvoted as the most directly answring the OP: the

+  [a,b]

(with single bracket around the addend) is like list.extend([a,b])

       [ 1, 2, 3,4]  + [5,6]

Out[6]: [1, 2, 3, 4, 5, 6]

Whereas

+  [[a,b]]

(with double brackets) is like list.append([a,b])

In [5]:      [ 1, 2, 3,4]  + [[5,6]]
Out[5]: [1, 2, 3, 4, [5, 6]]
1
votes

Well although the answers above me are correct, another nice(yet maybe not pretty) way to do that would be using the fact that append returns None:

func(l.append([]) is None and l[-1])

Which practically let's you do whatever you want in the same line with .append