Slicing a list with non-integer parameter? How?
Emile van Sebille
emile at fenx.com
Sat Oct 6 11:40:17 EDT 2001
list1.remove(2)
and
list2.remove('thrie')
-- vs 'drie'
HTH,
Emile van Sebille
emile at fenx.com
---------
----- Original Message -----
From: "Husam" <h.jehadalwan at student.kun.nl>
Newsgroups: comp.lang.python
Sent: Saturday, October 06, 2001 8:21 AM
Subject: Slicing a list with non-integer parameter? How?
> Hi fiends,
> I'm trying to determine the position of an item in a list in order to
> delete it by slicing the list with non integer parameter.
> Code I works just fine. But code II doe's not work:
>
> Code 1 with integer values:
>
> >> list1=[1,2,3,4]
> >>> for i in list1:
> ... if i==2:
> ... pos=len(list)-len(list[i:])
> ... del list[pos-1]
> ... print list
> ...
> [1, 3, 4]
>
> Code II with non integer values:
>
> >>> list2=['one','two','thrie','four']
> >>> for i in list2:
> ... if i=='drie':
> ... pos=len(list)-len(list[i:])
> ... del list[pos-1]
> ... print list
> ...
> Traceback (innermost last):
> File "<stdin>", line 3, in ?
> TypeError: slice index must be int
>
>
> PS: my Python version is 1.5
>
> thanks in advance
>
>
>
More information about the Python-list
mailing list