# list item's position

Discussion in 'Python' started by Bob Smith, Jan 20, 2005.

1. ### Bob SmithGuest

Hi,

I have a Python list. I can't figure out how to find an element's
numeric value (0,1,2,3...) in the list. Here's an example of what I'm doing:

for bar in bars:
if 'str_1' in bar and 'str_2' in bar:
print bar

This finds the right bar, but not its list position. The reason I need
to find its value is so I can remove every element in the list before it
so that the bar I found somewhere in the list becomes element 0... does
that make sense?

Thanks,

Bob

Bob Smith, Jan 20, 2005

2. ### Mark McEahernGuest

Bob Smith wrote:

> Hi,
>
> I have a Python list. I can't figure out how to find an element's
> numeric value (0,1,2,3...) in the list. Here's an example of what I'm
> doing:

Use enumerate() (new in Python 2.3, IIRC). Otherwise:

for i in range(len(sequence)):
item = sequence
...

>
> for bar in bars:
> if 'str_1' in bar and 'str_2' in bar:
> print bar
>
> This finds the right bar, but not its list position. The reason I need
> to find its value is so I can remove every element in the list before
> it so that the bar I found somewhere in the list becomes element 0...
> does that make sense?

Sure. You want to slice the list starting at the index of the first
occurrence:

index = min([i for i, item in enumerate(sequence) if 'str_1' in item and
'str_2' in item])
print sequence[index:]

// m

Mark McEahern, Jan 20, 2005

3. ### Bill MillGuest

2 solutions:

In [98]: bars = ["str", "foobaz", "barbaz", "foobar"]

In [99]: for bar in bars:
....: if 'bar' in bar and 'baz' in bar:
....: print bar
....: print bars.index(bar)
....:
barbaz
2

In [100]: for i in range(len(bars)):
.....: if 'bar' in bars and 'baz' in bars:
.....: print bars
.....: print i
.....:
barbaz
2

The first one is slow and pretty, the second one is fast and (a bit)
ugly. I believe that you should avoid range(len(x)) when you can, but
use it when you need to know the index of something without an

Peace
Bill Mill
bill.mill at gmail.com

On Wed, 19 Jan 2005 22:04:44 -0500, Bob Smith
<> wrote:
> Hi,
>
> I have a Python list. I can't figure out how to find an element's
> numeric value (0,1,2,3...) in the list. Here's an example of what I'm doing:
>
> for bar in bars:
> if 'str_1' in bar and 'str_2' in bar:
> print bar
>
> This finds the right bar, but not its list position. The reason I need
> to find its value is so I can remove every element in the list before it
> so that the bar I found somewhere in the list becomes element 0... does
> that make sense?
>
> Thanks,
>
> Bob
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Bill Mill, Jan 20, 2005
4. ### SeanGuest

Not sure if this is what you are looking for but...

>>> li = ['this','is','a','list','of','strings']
>>> li = [l for l in li if li.index(l) >= li.index('a')]
>>> li

['a', 'list', 'of', 'strings']
>>>

--
Sean Berry ~ Internet Systems Programmer
BuildingOnline Inc.
The Building Industry's Web Design and Marketing Agency
Celebrating our 9th year in business, founded Aug. 1995
Ph: 888-496-6648 ~ Fax: 949-496-0036
--> Web Design Agency site: http://www.BuildingOnline.net
--> Building Industry Portal: http://www.BuildingOnline.com
--> Building Industry News: http://www.BuildingOnline.com/news/
--> Home Plans: http://www.eHomePlans.com
"Bob Smith" <> wrote in message
news:csn747\$3fq\$...
> Hi,
>
> I have a Python list. I can't figure out how to find an element's numeric
> value (0,1,2,3...) in the list. Here's an example of what I'm doing:
>
> for bar in bars:
> if 'str_1' in bar and 'str_2' in bar:
> print bar
>
> This finds the right bar, but not its list position. The reason I need to
> find its value is so I can remove every element in the list before it so
> that the bar I found somewhere in the list becomes element 0... does that
> make sense?
>
> Thanks,
>
> Bob

Sean, Jan 20, 2005
5. ### Steven BethardGuest

Bill Mill wrote:
> 2 solutions:
>
> In [98]: bars = ["str", "foobaz", "barbaz", "foobar"]
>
> In [99]: for bar in bars:
> ....: if 'bar' in bar and 'baz' in bar:
> ....: print bar
> ....: print bars.index(bar)
> ....:
> barbaz
> 2
>
> In [100]: for i in range(len(bars)):
> .....: if 'bar' in bars and 'baz' in bars:
> .....: print bars
> .....: print i
> .....:
> barbaz
> 2
>
> The first one is slow and pretty, the second one is fast and (a bit)
> ugly. I believe that you should avoid range(len(x)) when you can, but
> use it when you need to know the index of something without an

See Mark's post, if you "need to know the index of something" this is
the perfect case for enumerate (assuming you have at least Python 2.3):

py> bars = ["str", "foobaz", "barbaz", "foobar"]
py> for i, bar in enumerate(bars):
.... if 'bar' in bar and 'baz' in bar:
.... print bar
.... print i
....
barbaz
2

The only time where I even consider using range(len(x)) is when I don't
also need to look at the item -- which I find to be quite uncommon...

Steve

Steven Bethard, Jan 20, 2005
6. ### Stephen ThorneGuest

On Wed, 19 Jan 2005 22:04:44 -0500, Bob Smith
<> wrote:
> Hi,
>
> I have a Python list. I can't figure out how to find an element's
> numeric value (0,1,2,3...) in the list. Here's an example of what I'm doing:
>
> for bar in bars:
> if 'str_1' in bar and 'str_2' in bar:
> print bar
>
> This finds the right bar, but not its list position. The reason I need
> to find its value is so I can remove every element in the list before it
> so that the bar I found somewhere in the list becomes element 0... does
> that make sense?

Given a list and a function:

def dropPredicate(x):
return not 'somecontents' in x

mylist = ['a', 'b', 'c', 'xxxxsomecontentsxxx', 'd', 'e', 'f']

import itertools

mylist = list(itertools.dropwhile(dropPredicate, mylist))

assert mylist == ['xxxxsomecontentsxxx', 'd', 'e', 'f']

This will drop everything at the start of the list for which
'dropPredicate' returns true. This will mean that even if
dropPredicate returns false for more than one element of the list, it
will stop at the first element. If there are no elements for which
dropPredicate returns true, the result will be an empty list.

Regards,
Stephen Thorne.

Stephen Thorne, Jan 20, 2005
7. ### John MachinGuest

On Wed, 19 Jan 2005 22:02:51 -0700, Steven Bethard
<> wrote:

>
>See Mark's post, if you "need to know the index of something" this is
>the perfect case for enumerate (assuming you have at least Python 2.3):

But the OP (despite what he says) _doesn't_ need to know the index of
the first thingy containing both a bar and a baz, if all he wants to
do is remove earlier thingies.

def barbaz(iterable, bar, baz):
seq = iter(iterable)
for anobj in seq:
if bar in anobj and baz in anobj:
yield anobj
break
for anobj in seq:
yield anobj

>>> import barbaz
>>> bars = ["str", "foobaz", "barbaz", "foobar"]
>>> print list(barbaz.barbaz(bars, 'bar', 'baz'))

['barbaz', 'foobar']
>>> print list(barbaz.barbaz(bars, 'o', 'b'))

['foobaz', 'barbaz', 'foobar']
>>> print list(barbaz.barbaz(bars, '', 'b'))

['foobaz', 'barbaz', 'foobar']
>>> print list(barbaz.barbaz(bars, '', ''))

['str', 'foobaz', 'barbaz', 'foobar']
>>> print list(barbaz.barbaz(bars, 'q', 'x'))

[]
>>>

John Machin, Jan 20, 2005
8. ### Bengt RichterGuest

On Wed, 19 Jan 2005 22:04:44 -0500, Bob Smith <> wrote:

>Hi,
>
>I have a Python list. I can't figure out how to find an element's
>numeric value (0,1,2,3...) in the list. Here's an example of what I'm doing:
>
>for bar in bars:
> if 'str_1' in bar and 'str_2' in bar:
> print bar
>
>This finds the right bar, but not its list position. The reason I need
>to find its value is so I can remove every element in the list before it
>so that the bar I found somewhere in the list becomes element 0... does
>that make sense?
>

sneaky (python 2.4) one-liner (not tested beyond what you see, and not recommended
as the best self-documenting version ;-)

>>> bars = [

... 'zero',
... 'one',
... 'str_1 and str_2 both in line two',
... 'three',
... 'four',
... 'str_1 and str_2 both in line five',
... 'last line']

>>> newbar=bars[sum(iter(('str_1' not in bar or 'str_2' not in bar for bar in bars).next, 0)):]
>>> for s in newbar: print repr(s)

...
'str_1 and str_2 both in line two'
'three'
'four'
'str_1 and str_2 both in line five'
'last line'

Alternatively:

>>> newbar=bars[[i for i,v in enumerate(bars) if 'str_1' in v and 'str_2' in v][0]:]
>>> for s in newbar: print repr(s)

...
'str_1 and str_2 both in line two'
'three'
'four'
'str_1 and str_2 both in line five'
'last line'

Alternatively:

>>> newbar = list(dropwhile(lambda x: 'str_1' not in x or 'str_2' not in x, bars))
>>> for s in newbar: print repr(s)

...
'str_1 and str_2 both in line two'
'three'
'four'
'str_1 and str_2 both in line five'
'last line'

Regards,
Bengt Richter

Bengt Richter, Jan 20, 2005