[Tutor] list method help
Alan Gauld
alan.gauld at freenet.co.uk
Thu Feb 2 19:17:42 CET 2006
> list = ["1", "test", "1.5"]
> for x in list:
> print list.pop(x)
>
> I get the following error:
>
> print list.pop(x)
> TypeError: an integer is required
>
> Does this mean i can't use a for loop to pop things from a list?
No it means pop takes an optional *index* as an argument.
> list = ["1", "test", "1.5"]
> for x in list:
> print list.pop(0)
>
> which worked but returned the following:
>
> 1
> test
>
> Why did it not return the last value in the list?
Because you provided an index of 0 which is the beginning.
Try
list = ["1", "test", "1.5"]
for x in list:
print list.pop()
Or maybe more usefully:
for x in range(len(list)):
print list.pop()
BTW list is a bad name for a variable since it masks the
builtin list() function.
HTH,
Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list