473,671 Members | 2,370 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

List index - why ?

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

Jul 18 '05 #1
2 1175
On Thu, 27 Nov 2003 10:48:04 +0100, Kepes Krisztian wrote:
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.
--
mailto: logan@phreaker( NoSpam).net

Jul 18 '05 #2
Kepes Krisztian wrote:

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
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
7951
by: Jeffrey Silverman | last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree structure from that list. Basically, I want to turn this: $list = array( array( 'id' => 'A', 'parent_id' => null, 'value' => 'aaa') , array( 'id' => 'B', 'parent_id' => 'A', 'value' => 'bbb') , array( 'id' => 'C', 'parent_id' => 'B', 'value' => 'ccc') , array( 'id' => 'D', 'parent_id' => 'A', 'value' => 'ddd')
21
4313
by: Hilde Roth | last post by:
This may have been asked before but I can't find it. If I have a rectangular list of lists, say, l = ,,], is there a handy syntax for retrieving the ith item of every sublist? I know about for i in l] but I was hoping for something more like l. Hilde
9
2547
by: Jess Austin | last post by:
hi, I like the way that Python does lists, and I love the way it does iterators. But I've decided I don't like what it does with iterators of lists. Lists are supposed to be mutable sequences, but try to use an iterator of a list that you're mutating and watch it all fall to pieces. That is, if you change the length of a section of the list through which the iterator has already passed, it will lose track of where it is. I think...
9
1661
by: danny van elsen | last post by:
hello all, I have an application in which I build a list<node>, with potentially thousands of nodes. each node has an "index", and all nodes are ordered by this index. this index reflects a value that has been computed before, and will range from 0 to N. node 0: index 0
5
24768
by: Joe Fallon | last post by:
I have a list box with 7 text values in it. I have a pair of buttons to Move Up or Move Down the selected item one position. What is the simplest way to code the buttons so the item moves one position? -- Joe Fallon
9
17399
by: William Meyer | last post by:
hi, I need to get the index of an object in a list. I know that no two objects in the list are the same, but objects might evaluate as equal. for example list = for object in list: objectIndex = list.index(object) print objectIndex
6
7969
by: Michael McGarry | last post by:
Hi, Please excuse me if this is not the right forum for this question. I would like to create a random permutation of a list of numbers. How can I do this in C? I was just going to draw a number from 1 to n. n is the number of items in the list and place the items in a new list in the order that their index is drawn from the random number generator. Is there a library function to do this?
12
3944
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1 and list2, each node within list1 has various data and needs to have a pointer to the corresponding node in list2, but I cant figure out how to do this. Could someone explain what I might be missing, or maybe point me in the direction of a good...
15
3092
by: Macca | last post by:
Hi, My app needs to potentially store a large number of custom objects and be able to iterate through them quickly. I was wondering which data structure would be the most efficient to do this,a hashtable or a generic list. Is using enumerators to iterate through the data structure a good idea? I'd appreciate any suggesstions or advice,
4
2529
by: valeberry | last post by:
//Index.php <html><head><title>Mailing List Administration</title></head><body> <br> <center><H1>Mailing List Administration</H1></center> Send an email to a mailing list: <form method=post action="sendemail.php3"> <table><tr><td> <b>From Address:</b> <input type=text name="From" size="40" value=""> <br>
0
8819
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8596
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8667
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7428
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4221
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2806
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.