List index - why ?

K

Kepes Krisztian

Hi !

A.)

The string object have a method named "index", and have a method named
"find".
It is good, because many times we need to find anything, and it is
very long to write this:

try:
i=s.index('a')
except:
i=-1
if i<>-1: pass

and not this:

if (s.find('a')<>-1): pass

Why don't exists same method in the list object ?

It is very ugly thing (sorry, but I must say that).

I must write in every times:

l=[1,2,3,4]
try:
i=l.index(5)
except:
i=-1
if i<>-1: pass

and not this:
if (l.find(5)<>-1): pass

B.)

Same thing is the deleting.

I think, this method is missing from strings, and lists.

Example:

I must write this:

s='abcdef'
l=[1,2,5,3,4,5]

print s
s=s[:2]+s[3:]
print s

print l
l[2]=None
l.remove(None)
print l

and not this:
s='abcdef'
l=[1,2,5,3,4,5]
s=s.delete(2)
l.delete(2)


So: some functions/methods are neeeded to Python-like programming
(less write, more effectivity).


KK
 
L

Logan

The string object have a method named "index", and have a method named
"find".
It is good, because many times we need to find anything, and it is
very long to write this:

try:
i=s.index('a')
except:
i=-1
if i<>-1: pass

and not this:

if (s.find('a')<>-1): pass

Why don't exists same method in the list object ?

It is very ugly thing (sorry, but I must say that).

I must write in every times:

l=[1,2,3,4]
try:
i=l.index(5)
except:
i=-1
if i<>-1: pass

and not this:
if (l.find(5)<>-1): pass

You can simply use the 'in' operator:

a = "test"
b = ['t', 'e', 's', 't']

if 's' in a: ...
if 's' in b: ...

'find' and 'index' will give you the position of the first
occurrence of what you are looking for; but 'find' will return
-1 if what you are looking for is not in the string whereas 'index'
will return a ValueError (s. example below). If you just want to
test, whether something is in a string or a list, use 'in'.

's' in a --> True
's' in b --> True
a.find('s') --> 2
b.index('s') --> 2

'z' in a --> False
'z' in b --> False
a.find('z') --> -1
a.index('z') --> ValueError

HTH, L.
 
P

Peter Hansen

Kepes said:
The string object have a method named "index", and have a method named
"find".
Why don't exists same method in the list object ?

It does:

Python 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on win32
l = [1, 2, 3]
l = list('abcdefg')
l ['a', 'b', 'c', 'd', 'e', 'f', 'g']
l.index('d')
3

..find() doesn't exist on lists, perhaps because nobody has asked for it
and presented a good use case. It seems to me quite rare that somebody
needs to search for a sublist in a list, and you can already search for
individual elements with "in".

Same thing is the deleting.
I think, this method is missing from strings, and lists.

Strings are immutable, and you can already delete elements
from lists. Or did I miss something... your request was
a little confusing.

-Peter
 

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

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,473
Latest member
pioneertraining

Latest Threads

Top