About 1,370,000 results
Open links in new tab
  1. python - How can I find the index for a given item in a list? - Stack ...

    Caveats Linear time-complexity in list length An index call checks every element of the list in order, until it finds a match. If the list is long, and if there is no guarantee that the value will be near the …

  2. python - How to find all occurrences of an element in a list - Stack ...

    index() will give the first occurrence of an item in a list. Is there a neat trick which returns all indices in a list for an element?

  3. python - How do I get the last element of a list? - Stack Overflow

    Jun 14, 2019 · Getting a list when you want an element only postpones the inevitable "list index out of range" - and that's what should happen when attempting to get an element from an empty list.

  4. Best way to handle list.index (might-not-exist) in python?

    forms a typical fork within the happy path of many programs. This design flaw forces programmers to pick the lesser of two evils: 1) sacrifice performance and iterate through the list twice to get the index …

  5. python - Getting the index of the returned max or min item using max ...

    Mar 19, 2010 · 735 I'm using Python's max and min functions on lists for a minimax algorithm, and I need the index of the value returned by max() or min(). In other words, I need to know which move …

  6. Finding the index of elements based on a condition using python list ...

    In Python, you wouldn't use indexes for this at all, but just deal with the values— [value for value in a if value > 2]. Usually dealing with indexes means you're not doing something the best way. If you do …

  7. python - using index () on multidimensional lists - Stack Overflow

    return -1,-1#not found print m_array_index(data, 6) Or with all occurrences (sure code could be optimized - modified to work with generators and so on - but here is just a sample):

  8. How to find the index of n largest elements in a list or np.array, Python

    Jun 2, 2013 · Is there a built-in function or a very simple way of finding the index of n largest elements in a list or a numpy array? K = [1,2,2,4,5,5,6,10] Find the index of the largest 5 elements?

  9. Getting indices of True values in a boolean list - Stack Overflow

    The range here enumerates elements of your list and since we want only those where self.states is True, we are applying a filter based on this condition. For Python > 3.0:

  10. How to remove an element from a list by index - Stack Overflow

    Dec 13, 2016 · How do I remove an element from a list by index? I found list.remove(), but this slowly scans the list for an item by value.