Rita Sue and Bob too

Peter Hansen peter at engcorp.com
Fri Aug 20 04:14:53 CEST 2004

M. Clift wrote:
> Hi All,
> Can someone help. I promise I've looked how to do this but can't find a
> way...
> Ok, to find one name is easy
> if 'Bob' in list:
>     print "They were found"
> else:
>     print "They are not in list"
> But, how to I find a sequence in a list of unknown size? i.e. this sequence
> in list of other names and replace it with three others?
> 'Rita','Sue','Bob'
> This is almost a nightly occurrence (my posting questions), but I am
> learning : )

My first thought would be to scan the list for the first element
'Rita', and when you find it, take out a slice that is the
same size as the sequence you have there (use len() of course)
and compare the two.  If they don't match, move on.  The scanning
and "moving on" part would certainly best be done using .index()
and note that that function can take a second parameter which
specifies the starting index, so you can do a progressive search
efficiently.  The actual code is left as an exercise to the
reader. ;-)


More information about the Python-list mailing list