Posted inPython

How to find the element in python list?

Today, We want to share with you python find element in list .In this post we will show you python find index of item in list containing string, hear for Python : How to Check if an item exists in list ? we will give you demo and example for implement.In this post, we will learn about python list contains with an example.

Python List index() – Get Index or Position of Item in List

list.index(element, start, end)

Find the index of the element

Example 1:


vowels = ['a', 'e', 'i', 'o', 'i', 'u']


index = vowels.index('e')
print('The index of e:', index)

index = vowels.index('i')

print('The index of i:', index)

Example 2: Checking if something is inside

3 in [1, 2, 3] # => True

Example 3:
Python3 program to Find elements of a
list by indices present in another list

def findElements(lst1, lst2): 
	return [lst1[i] for i in lst2] 
			 
lst1 = [10, 20, 30, 40, 50] 
lst2 = [0, 2, 4] 
print(findElements(lst1, lst2)) 

Using numpy

import numpy as np 

def findElements(lst1, lst2): 
	return list(np.array(lst1)[lst2]) 
			 
lst1 = [10, 20, 30, 40, 50] 
lst2 = [0, 2, 4] 
print(findElements(lst1, lst2)) 

Using itemgetter()


from operator import itemgetter 

def findElements(lst1, lst2): 
	return list((itemgetter(*lst2)(lst1))) 
			 
lst1 = [10, 20, 30, 40, 50] 
lst2 = [0, 2, 4] 
print(findElements(lst1, lst2)) 

Using Python map()


def findElements(lst1, lst2): 
	return list(map(lst1.__getitem__, lst2)) 
			
lst1 = [10, 20, 30, 40, 50] 
lst2 = [0, 2, 4] 
print(findElements(lst1, lst2)) 

I hope you get an idea about Finding the index of an item in a list.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype