> On Wed, 19 Jan 2005 22:04:44 -0500, Bob Smith wrote: > > [...] 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?
I have borrowed the bars list example from "Bill Mill"s solutions and here are the two solutions... bars = ['str', 'foobaz', 'barbaz', 'foobar'] # Solution 1: The enumerate(). for idx, bar in enumerate(bars): if 'bar' in bar and 'baz' in bar: break bars_sliced = bars[idx:] print bars_sliced # Solution 2: Low level approach, testing and removing combined. In situ. while bars: # some prefer len(bars) > 0, which is less magical, IMHO bar = bars.pop(0) # get and remove the first element if 'bar' in bar and 'baz' in bar: bars.insert(0, bar) # insert the tested back break # and finish print bars The result is ['barbaz', 'foobar'] in both cases. Petr -- Petr Prikryl (prikrylp at skil dot cz) -- http://mail.python.org/mailman/listinfo/python-list