append function problem?

R

randomtalk

hello, recently i tried to use list.append() function in seemingly
logical ways, however, i cannot get it to work, here is the test code:
seed = [2, 3, 4, 5]
next = 7
seed1 = seed.append(next)
seed1
print(str(seed1)) None
def test(lst):
.... print(str(lst))
....None

I'm using Activestate python (latest) on win xp sp2..

I'm not sure why seed1 and the function doesn't recognize the list..

If anyone can point out where i'm going wrong would be much
appreciated!
Thanks in advance!
 
M

Murali

A typo here? seed v/s seed1.

Instead of "print(seed.append(5))", try "seed.append(5)" followed by
"print seed" -- "print(seed)" also works. The append method does not
return the appended value
(like many C functions).

- Murali
 
J

John Machin

hello, recently i tried to use list.append() function in seemingly
logical ways, however, i cannot get it to work, here is the test code:
seed = [2, 3, 4, 5]
next = 7
seed1 = seed.append(next)
seed1
print(str(seed1))
None

(1) Try this:

print seed

(2) Read the documentation (especially the HEADING and the FIRST sentence):

http://docs.python.org/lib/typesseq-mutable.html

(3) Work your way through the Python Tutorial; here's the section on
lists, which covers your problem:

http://docs.python.org/tut/node5.html#SECTION005140000000000000000
... print(str(lst))
...
None

I'm using Activestate python (latest) on win xp sp2..

I'm not sure why seed1 and the function doesn't recognize the list..

I'm not sure what "doesn't recognize the list" means.
 
V

vbgunz

seed = [1,2,3]
seed.append(4)
print seed # [1,2,3,4]

many of the list methods are in place methods on a mutable object. In
other words, doing the following results in None.

seed = [1,2,3]
seed = seed.append(4)
print seed # None

you also just wiped out your list... The append method like many other
list methods simply return None. To get the value of an append, append
first then access later like so.

seed = [1,2,3]
seed.append(4)
print seed # [1,2,3,4]
 
B

*binarystar*

# Try This
seed = [2, 3, 4, 5]
next = [7]
seed1 = seed + next



hello, recently i tried to use list.append() function in seemingly
logical ways, however, i cannot get it to work, here is the test code:
seed = [2, 3, 4, 5]
next = 7
seed1 = seed.append(next)
seed1
print(str(seed1)) None
def test(lst):
... print(str(lst))
...None

I'm using Activestate python (latest) on win xp sp2..

I'm not sure why seed1 and the function doesn't recognize the list..

If anyone can point out where i'm going wrong would be much
appreciated!
Thanks in advance!
 
B

bruno at modulix

hello, recently i tried to use list.append() function in seemingly
logical ways,

What seems logical and how it really works may not be the same... As a
general rule, for builtin types, destructive methods returns None. I
personnaly find it a wart, but what, it's the BDFL's choice.

(snip)
I'm not sure why seed1 and the function doesn't recognize the list..

because list.append() modifys the list in place and returns None.
 

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
474,294
Messages
2,571,511
Members
48,200
Latest member
SCPKatheri

Latest Threads

Top