468,734 Members | 2,298 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,734 developers. It's quick & easy.

append function problem?

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))
.... test(seed.append(next))

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!

Apr 28 '06 #1
5 1521
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

Apr 28 '06 #2
On 28/04/2006 1:49 PM, ra********@gmail.com wrote:
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.htm...00000000000000
def test(lst): ... print(str(lst))
... test(seed.append(next))

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.

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

Apr 28 '06 #3
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]

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

ra********@gmail.com wrote:
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))
... test(seed.append(next))

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!

Apr 28 '06 #5
ra********@gmail.com wrote:
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.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 28 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by waters | last post: by
5 posts views Thread by solar | last post: by
2 posts views Thread by jeremito | last post: by
2 posts views Thread by kvnil | last post: by
1 post views Thread by CARIGAR | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.