By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,137 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

append function problem?

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
# 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

P: n/a
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.