Skip to main content
added 409 characters in body
Source Link
Bill Bell
  • 21.3k
  • 6
  • 46
  • 61

By using

(index for index, value in enumerate(the_iterable) if condition(value))

one can check the condition of the value of the first item in the_iterable, and obtain its index without the need to evaluate all of the items in the_iterable.

The complete expression to use is

first_index = next(index for index, value in enumerate(the_iterable) if condition(value))

Here first_index assumes the value of the first value identified in the expression discussed above.

first_index = next(index for index, value in enumerate(the_iterable) if condition(value))

By using

(index for index, value in enumerate(the_iterable) if condition(value))

one can check the condition of the value of the first item in the_iterable, and obtain its index without the need to evaluate all of the items in the_iterable.

The complete expression to use is

first_index = next(index for index, value in enumerate(the_iterable) if condition(value))

Here first_index assumes the value of the first value identified in the expression discussed above.

Source Link
aris
  • 28.6k
  • 10
  • 80
  • 105

first_index = next(index for index, value in enumerate(the_iterable) if condition(value))