Python find index of element in list

In this Python tutorial, we will learn how to find the index of elements in a list by using Python. Also, we will cover these topics.

  • Python find index of element in list closest to value
  • Python find index of element in list greater than
  • Python find index of element in list of lists
  • Python find index of element in list with condition
  • Python find index of item in list
  • Python find index of object in list by attribute
  • Python find index of item in list containing string
  • Python get index of element in list by value
  • Python find index of item in list without exception
  • Python find index of item in list case insensitive
  • Python get index of item in list while iterating
  • Python get index of item in list with default
  • Python get index of element in list if exists
  • Python find index of item in list regex
  • Python find position of item in a list
  • Python get index of max element in a list.
  • Python find index of smallest element in list
  • Find index of duplicate elements in list Python
  • Python find index of multiple elements in list
  • Python get index of element in list NumPy
  • Python find index of common elements in two lists
  • Python get index of item in list comprehension

Python find index of element in list

  • In this Program, we will discuss how to find the index of element in a list by using Python.
  • In Python to get the index of item in list. we are going to use a method list.index() and this function will always return the index of a particular item in the list and this is a in-built() function in Python.
  • In Python lists are basically collection of items and each item has its unique positions and these positions are called as indexes. Now by using the list.index() function we are able to get the index of an item in a list.

Syntax:

Here is the Syntax of the index() method

list.index(
           element,
           start,
           end
          )
  • It consists of few Parameters
    • element: This parameter indicates that which element you would like to find.
    • start: It is a optional parameter and it will help you to start the search.
    • end: This parameter specifies if you provide ‘end’ argument then the search will end at the index.

Example:

Let’s take an example and check how to find the index of an element in a list

new_lis = [82, 64, 189, 920, 452, 701]

new_val = 920
result = new_lis.index(new_val)
print(result)

In the above code, we have created a list with integer numbers and then we will find the index of ‘920’ in the given list. To do this task first we create a variable ‘result’ and then assign a list.index() method to get the index value of that number.

Here is the Output of the following given code

Python find index of element in list
Python find an index of an element in a list

Read Square Root in Python

Get index of elements in a Python list

  • Here in this example, we are going to use the combination of a for-loop and append() method to get the index of elements in a Python list.
  • To get the index of an element in a list first, we will create a list ‘lis_1’ and now we want to get the index number of ‘193’ which is present in the list and now I want all the indexes with the number of ‘193’.

Source Code:

lis_1 = ['45', '92', '193', '193', '231', '190'] 

emp_lis = [] 
for m in range(0, len(lis_1)) : 
    if lis_1[m] == '193' : 
        emp_lis.append(m)
print("Index no of '193':", emp_lis)

Here is the implementation of the following given code

Python find index of element in list
Python find the index of an element in a list

As you can see in the Screenshot the element is present at 2 and 3 positions.

By using NumPy method

  • In this example, we have especially used the concept of a Python numpy array to get the index of an element in a list. So to do this task first we will create a list and then use the np.array() function to convert the list into an array. Now we want to get the index of ‘119’ number which is available in the list.
  • To do this we can apply the concept np.where() method and this function will help the user to select elements from the given array based on the condition.

Source Code:

import numpy as np

new_list = [56,829,992,119,734,567,890] 
arr = np.array(new_list)
result = np.where(arr == 119)[0]
print(result)

You can refer to the below Screenshot

Python find index of element in list
Python find the index of an element in a list

As you can see in the Screenshot the element ‘119’ is present at 3 positions.

This is how to use the NumPy method to get the index of elements in a Python list.

Also, check: Python find number in String

Python find index of element in list closest to value

  • Let us see how to find an index of elements in a list that is closest to value.
  • By using the min() method we can apply a key that searches the difference of each item with K, and this method will always return the item which having less number of difference.

Example:

new_lis = [21.34,15.4567,18.478,22.105,13.189]

b = min(range(len(new_lis)), key=lambda m: abs(new_lis[m]-17.6))
print(b)

Here is the Screenshot of the following given code

Python find index of element in list closest to value
Python find an index of the element in the list closest to value

Read: Remove non-ASCII characters Python

Python find index of element in list greater than

In this example, we will use a Python lambda function to equate with the given value at each index and then, filter out the given condition. This method actually sets the index value as 0 and specifies the first item greater than the value.

Example:

my_lis = [109,134,12,83,10]

new_output = list(filter(lambda m: m > 120, my_lis))[0]
print(my_lis.index(new_output))

In the above code first, we have initialized a list ‘my_lis’ and then used the filter+ lambda() function to get the index of elements in a list that is greater than ‘120’.

Once you will print ‘new_output’ then the output will display the index number which is greater than ‘120’ that is ‘1’:134.

Here is the Output of the following given code

Python find index of element in list greater than
Python find an index of the element in list greater than

Read: Python convert binary to decimal

Python find index of element in list of lists

  • In this Program, we will discuss how to find the index of elements in the list of lists in Python.
  • To perform this particular task we are going to apply the list.index() method and in this example, we will check the condition if the item is not available in the list then the index() method will raise an error.
  • In the below example we have created a list of lists and we have to find the index number of ‘321’ in the given list.

Source Code:

new_lis_lis = [["98","85","112","321","405"],["189","239","706","148","227"]]
def find(m):
    for x, new_val in enumerate(new_lis_lis):
        try:
            y = new_val.index(m)
        except ValueError:
            continue
        yield x, y

new_result = [z for z in find('321')]
print(new_result)

Here is the execution of the following given code

Python find index of element in list of lists
Python find an index of the element in a list of lists

As you can see in the Screenshot the element is present at 3 positions.

Read: Python Count Words in File

Python find index of element in list with condition

  • Let us see how to find the index of elements in the list with conditions in Python.
  • By using the list comprehension method we can easily get the index number of specific elements. In Python, list comprehension is used to iterate items through a given list and access its indexes along with values.
  • It will check the condition in len() method if no argument is passed then it will return an empty list. This method will actually help the user to reduce longer loops and make the program easier.

Example:

new_lis = [1877,18,77,30,77,45,567]

output = [ i for i in range(len(new_lis)) if new_lis[i] == 77 ]
print("Get index number from list:",output)

In the above code, we have to find the index number of ’77’ by using list comprehension. Once you will print ‘output’ then the result will display the index number of ’77’ that is 2 and 4.

You can refer to the below Screenshot

Python find index of element in list with condition
Python find an index of an element in the list with condition

Also, check: How to Find Duplicates in Python DataFrame

Python find index of item in list

  • In Python to get the index of items in a list we can apply the list comprehension of len() class with the item passed as a parameter.
  • In this example, we have to find the index of the first occurrence of values in a given list. First, we will create a list ‘new_lis’ and assign integer values to it.
  • Now use the len() method and give the condition if the value ’64’ is present in the list then it will display the index number otherwise it will return an empty list.

Source code:

new_lis = [73,123,64,52,74,86,32,64,64] 
 
output = [m for m in range(len(new_lis)) if new_lis[m] == 64] 
print("Find index of element in list:",output)

Here is the Screenshot of the following given code

Python find index of item in list
Python find an index of the item in a list

As you can see in the Screenshot the element is present at 2 7 and 8 positions.

Read: Case statement in Python

Python find index of item in list containing string

  • In this section, we will discuss how to find the index of items in the list containing string by using Python.
  • To get the index of an element in a list first, we will create a list of strings ‘new_list’ and now we want to get the index number of ‘Micheal’ which is present in the list. By default, the index value starts with 0.

Source Code:

new_list = ['George', 'Micheal', 'Micheal', 'John', 'Micheal', 'Potter'] 

emp_lis = [] 
for m in range(0, len(new_list)) : 
    if new_list[m] == 'Micheal' : 
        emp_lis.append(m)
print("Index no of 'Micheal':", emp_lis)

You can refer to the below Screenshot

Python find index of item in list containing string
Python find the index of an item in a list containing a string

Read: How to Reverse a List in Python

Python get index of element in list by value

  • In this Program, we will discuss how to get the index of elements in the list by value in Python.
  • By using the list.index() method, we can easily get the element index value from list. In this example we have define a list of integer values and uses the list.index() method we can get the index of the item whose value is ‘210’.

Example:

new_lis = [28, 190, 210, 567, 912, 678]

new_val = 210
result = new_lis.index(new_val)
print(result)

In the above code, we have created a list ‘new_lis’ and then initialize a variable ‘new_val’ in which we have to assign the value ‘210’. Now to find the index number of 210 we have to use the list.index() method.

Here is the implementation of the following given code

Python get index of element in list by value
Python get an index of an element in the list by value

Also, read: Get First Key in dictionary Python

Python find index of item in list without exception

  • In this Program, we will discuss how to get the index of elements in a list without using exception in Python.
  • To perform this task we can apply the Python enumerate() method and this function can be used to return the index number of all the elements which is present in the list.
  • In Python, the enumerator is basically allows us to iterate items through a sequence and this function takes in an iterable as a parameter such as lists.

Source Code:

my_new_lis = [56,92,17,39,115,189,459,512] 

new_output = [i for i, j in enumerate(my_new_lis) if j == 115] 
print("Element is present at index:",new_output)

You can refer to the below Screenshot

Python find index of item in list without exception
Python find an index of the item in a list without exception

As you can see in the Screenshot the element is present in the 4th position.

Finding an index of the item in a list without exception

  • In this example, we want to find the index of an element if it exists otherwise it will default value in Python.
  • If the value does not contain in list the it will return 2. In this example ‘420’ value is not found and a value error will be thrown.

Example:

new_element = [27,39,128,739,367,734]

try:
    new_val = new_element.index(420)
except ValueError:
    new_val = 2

print(new_val) 

Here is the Screenshot of the following given code

Python find index of item in list without exception
Python find an index of the item in a list without exception

This is how you can find the index of elements in the list without exception.

Read: Python dictionary increment value

Python find index of item in list case insensitive

  • In this section, we will discuss how to find the index of items in list case insensitive by using Python.
  • In this Program, you will convert the elements in the list to lower case by using the Python lower() method, and this function converts all uppercase characters into lowercase characters.
  • This method does not take any argument and always returns the lowercase string. In this example, we want to get the index number of the ‘S’ character. The index() method in the new_list has lower case element ‘s’.

Example:

Search_ele = 's'

my_new_lis1 =  ['M', 'Q', 'S', 'T', 'Z', 'H', 'W', 'A']
new_result = [item.lower() for item in my_new_lis1]
b = new_result.index(Search_ele.lower())
print(b)

Here is the execution of the following given code

Python find index of item in list case insensitive
Python find an index of the item in list case insensitive

As you can see in the Screenshot ‘s’ is available at the 2nd position.

Read: Python dictionary of lists

Python get index of item in list while iterating

  • Let us see how to get the index of items in the list while iterating by using Python.
  • By using the Python range() function we can easily iterate over the indices of a Python list. In Python this method will return a sequence of elements starting from 0.

Source Code:

new_lis = ['Micheal', 'John', 'Micheal']
 
for m in range(len(new_lis)):
    print((m, new_lis[m]))

Here is the implementation of the following given code

Python get index of item in list while iterating
Python get the index of an item in a list while iterating

Get the index of an item in a list while iterating

By using the enumerator() method we can solve this particular task and this method will help the user to iterate items through a sequence and this function takes in an iterable as a parameter such as lists.

Example:

list_value = [46, 81, 72, 39, 38, 1, 89,987,705,409]

for x, y in enumerate(list_value): 
    print(list((x, y))) 

You can refer to the below Screenshot

Python get index of item in list while iterating
Python get the index of an item in a list while iterating

Read: Python dictionary extend

Python get index of item in list with default

  • In this Program, we will discuss how to get the index of items in the list with default values by using Python.
  • In the given, we have set the default value as 0 for starting the index value. By using the combination of range and len() function we can easily get the index value of each element.

Source Code:

my_new_val = [78,39,117,84,47,290]

for z in range(0,len(my_new_val)):
    print([z])

Here is the Output of the following given code

Python get index of item in list with default
Python get the index of an item in a list with default

Python get index of element in list if exists

  • Here we can see how to get the index of elements in a list if exists by using Python.
  • In this example, we will use the combination of the ‘in’ operator and the index() method. In Python, the in operator will always check the condition if the value exists in the list then it will return true otherwise false. Now we are going to use the index() method and it will return the index of a particular item in the list.

Example:

Student_name = ['Olijah', 'William', 'Chris', 'James', 'Potter']
select_name = 'William'

if select_name in Student_name:
    new_output = Student_name.index(select_name)
    print("Element exist in list:",new_output)
else:
    print("Element does not exist in list:",select_name)

In the above code first, we have created a list and used an operator to check if the item ‘william’ exists in a list or not then use the index() method will get the index number of ‘william’ if it exists.

Here is the execution of the following given code

Python get index of element in list if exists
Python get an index of the element in a list if exists

As you can see in the Screenshot the element has 1st position.

Read: Python string to list

Python find index of item in list regex

  • In this section, we will discuss how to find an index of items in a list by using regular expression in Python.
  • In Python, the regex module is used to match the string based on the condition and the regex is imported through the re module.
  • In this example we use the concept of re.search() method and this function will search the regex pattern. It will check if the ‘china’ element exists in the list or not. If it exists then it will return the index number.

Source Code:

You can use the below snippet to execute this program

import re

new_lis1 = ['Germany', 'China', 'Switzerland']
new_output = [m for m, item in enumerate(new_lis1) if re.search('China', item)]
print("element index:",new_output)

Here is the Screenshot of the following given code

Python find index of item in list regex
Python find an index of the item in list regex

Read: Python Dictionary Copy

Python find a position of an item in a list

  • Let us see how to find a position of an item in a list by using Python.
  • In Python to find a position of an element in a list using the index() method and it will search an element in the given list and return its index.
  • In this example, we have to find the index number of ‘Mangoes’ to do this we are going to use the list.index() method.

Example:

fruit_name = ['Banana', 'Grapes', 'Mangoes', 'Apple']

result = fruit_name.index('Mangoes')
print("Position of element in list:",result)

You can refer to the below Screenshot

Python find a position of an item in a list
Python find a position of an item in a list

As you can see in the Screenshot the element is present in the 2nd position.

Read: Python NumPy Random

Python get index of max element in a list

  • In this program, we will discuss how to get the index of maximum element in a list.
  • In Python the max() method is used to find the maximum value in a given list and it accepts the sequence as a method parameter.
  • In this example we have also use the concept of list.index() method to find the index of a specific element from a list.

Source code:

list_elements = [89, 117, 734,864]

new_val = max(list_elements)
result = list_elements.index(new_val)
print("Maximum element index:",result)

In the above code, once you will print ‘result’ then the output will display the index number of the maximum element that is ‘864’.

Python get index of max element in a list
Python get the index of the max element in a list

As you can see in the Screenshot the maximum element is present in the 3rd position.

Read: Python dictionary multiple keys

Python find index of smallest element in list

  • Let us see how to find the index of the smallest element in a list in Python.
  • By using the Python min() method, we can easily get the minimum number that is available in the list along with we have used the index() method for finding the index of a specific element from the list.

Example:

min_list_my = [89, 64, 734,864,1167,23,45]

new_val = min(min_list_my)
result = min_list_my.index(new_val)
print("Smallest element index:",result)

You can refer to the below Screenshot

Python find index of smallest element in list
Python find an index of the smallest element in a list

As you can see in the Screenshot the minimum element is present in the 5th position.

Find index of duplicate elements in list Python

  • In this section, we will discuss how to find the index of duplicate elements in List by using Python.
  • By using the list comprehension and list slicing method we can easily get the index number of duplicate values which is present in the given list.

Source Code:

numbers_list = [83, 71, 83, 45, 83, 21, 11]

new_output = [m for m, new_val in enumerate(numbers_list) if new_val in numbers_list[:m]]
print("Duplicate element index:",new_output)

Here is the Screenshot of the following given code

Find index of duplicate elements in list Python
Find an index of duplicate elements in list Python

This is how to find the index number of duplicate values

Get the index number of duplicate values

By using the duplicated and where() method we can easily perform this particular task. In Python, duplicated() method checks the condition of whether duplicate values are available in the list or not, and the np.where() function returns the indices of duplicate values.

Source Code:

import numpy as np
import pandas as pd

my_list =  [78,489,55,78,92,78]
b=pd.DataFrame(my_list)
new_result = np.where(b.duplicated())
print("Index number of duplicate values:",new_result)

Here is the execution of the following given code

Find index of duplicate elements in list Python
Find an index of duplicate elements in list Python

Python find index of multiple elements in list

  • Here we can see how to find the index of multiple elements in a given list by using Python.
  • In this example, we have selected multiple values from a list and store them into a new list and then we apply the list comprehension method to get the index number of these values.

Source Code:

num_lis = [89,145,734,819]

result = {new_ele: m for m, new_ele in enumerate(num_lis)}
search_ele = [734,145]
b = [result.get(new_val) for new_val in search_ele]
print("Multiple index:",b)

In the above code, once you will print ‘b’ then the output will display the index number of ‘734’ and ‘145’.

You can refer to the below Screenshot

Python find index of multiple elements in list
Python find an index of multiple elements in a list

Python get index of element in list NumPy

  • In this Program, we will discuss how to get the index of elements in a list by using Python NumPy.
  • To do this task first we will create a list and then use the np.array() function to convert the list into an array. Now we want to get the index of ‘134’ number which is available in the list.

Source Code:

import numpy as np

new_numbers = [21,178,567,134,925,120,670] 
new_arr = np.array(new_numbers)
result = np.where(new_arr== 134)[0]
print(result)

Here is the execution of the following given code

Python get index of element in list NumPy
Python get an index of an element in list NumPy

As you can see in the Screenshot the element is present in the 3rd position.

Python find index of common elements in two lists

  • In this section, we will discuss how to find the index of common elements in two lists in Python.
  • First, we have created two lists and then initialize a variable ‘result’ in which we have assigned the set and the intersection method. This method will help us to get the common elements from the list. Now use the list.index() method to get the index number of those elements.

Example:

new_lis1=[87,56,36,77,58]
new_lis2=[56,44,39,12,13]

result = set(new_lis1).intersection(new_lis2)
index_lis1 = [new_lis1.index(m) for m in result]
index_lis2 = [new_lis2.index(m) for m in result]
print(index_lis1,index_lis2)

Here is the Output of the following given code

Python find index of common elements in two lists
Python find an index of common elements in two lists

Python get index of item in list comprehension

  • Let us see how to get the index of items in a list by using the Python list comprehension method. In Python, list comprehension method is used to iterate items through a given list and access its indexes along with values.
  • In this example, we will iterate over the list index and element by using the for loop method and it will check the condition if the element is ‘156’ then the value return with an index number.

Example:

int_numbers = [78, 68, 156, 289, 213, 71]

new_output = [z for z in range(len(int_numbers)) if int_numbers[z] == 156]
print(new_output)

Here is the Screenshot of the following given code

Python get index of item in list comprehension
Python get the index of an item in a list comprehension

This is how to get the index of an item in a list by using the Python list comprehension method.

You may also like the following Python tutorials:

In this Python tutorial, we have learned how to find the index of elements in a list by using Python. Also, we have covered these topics.

  • Python find index of element in list closest to value
  • Python find index of element in list greater than
  • Python find index of element in list of lists
  • Python find index of element in list with condition
  • Python find index of item in list
  • Python find index of object in list by attribute
  • Python find index of item in list containing string
  • Python get index of element in list by value
  • Python find index of item in list without exception
  • Python find index of item in list case insensitive
  • Python get index of item in list while iterating
  • Python get index of item in list with default
  • Python get index of element in list if exists
  • Python find index of item in list regex
  • Python find position of item in a list
  • Python get index of max element in a list.
  • Python find index of smallest element in list
  • Find index of duplicate elements in list Python
  • Python find index of multiple elements in list
  • Python get index of element in list NumPy
  • Python find index of common elements in two lists
  • Python get index of item in list comprehension