If you are working as Python developer, data analysts or data scientists for any organisation then it is very important for you to know how to play with Lists and get the requested info such as matching indexes or items from them.
In Python, Lists store an ordered collection of items which can be of different types. Each item in a list has an assigned index value. It is important to note that Python is a zero indexed based language. All this means is that the first item in the list is at index 0.If you want to get all the occurrences and the position of one or more items in a list by using Python then there are many ways but you need a very sufficient way to get the matching items from the list.
Let’s see the below example —
MyList = ['Mango', 'Orange', 'Banana', 'Apple', 'Grapes', 'Apple']
In the above list, you can see that Python based list is starting with 0 index and end with total item-1. In our cases, last index value in our fruit list will be 6–1 =5
Now, this point is How to get the occurrence of a item in the list. You could use a list comprehension with enumerate too such as given below -
# list of items
MyList = ['Mango', 'Orange', 'Banana', 'Apple', 'Grapes', 'Apple']#searchable item value
SeachItem='Apple'#store matching values through enumerate into a list comprehension
indexes = [n for n, x in enumerate(MyList) if x==SeachItem]#matching indexed value
indexes[3, 5]
# pandas library for data manipulation in python
import pandas as pd# list of items
MyList = ['Mango', 'Orange', 'Banana', 'Apple', 'Grapes', 'Apple']#convert list into series variable
series = pd.Series(MyList)#values in series variable
series# Display output0 Mango
1 Orange
2 Banana
3 Apple
4 Grapes
5 Apple
Now, we have converted list into series by using pandas library or package and we will be able to get a comparison check which will return a series of Booleans:
#searchable item value
SeachItem='Apple'#comparison check
series == SeachItem# Display output
0 False
1 False
2 False
3 True
4 False
5 True
If you try to pass that series of Booleans to the series via subscript notation, and you get just the matching members:
series[series == SeachItem]
# Display output
3 Apple
5 Apple
dtype: objectIn this case, if you want them in a list or tuple, just pass them to the constructor as given below:
#list or tuple
list(series[series == SeachItem].index)# Display output
[3, 5]
Through For Loop — This is the another options to get the matching items from a given list, only for those coming from another language and may be with a simple loop it’s easier to understand and use it:
# list of items
MyList = ['Mango', 'Orange', 'Banana', 'Apple', 'Grapes', 'Apple']#searchable item valie
SeachItem='Apple'# converting a list comprehension
mlist = enumerate(MyList)#for loop
for index, item in mlist:
if item == SeachItem:
print(index, item)
# Display output
3 Apple
5 Apple


Interesting thoughts of Python index and matching of item list. I wonder, does e games online developer do these kind of indexing? Or they have some other other language aside from python
ReplyDeleteThe article explains how Python lists use zero-based indexing and demonstrates an efficient approach for finding all occurrences of an item using the enumerate() function combined with list comprehensions. Understanding list indexing and iteration techniques enables developers to efficiently search, filter, and manipulate collections, making these concepts fundamental for Python programming, scripting, data analysis, and automation tasks.
ReplyDeletePython lists and built-in functions are widely used in data manipulation, automation, and preprocessing workflows because they provide flexible and efficient ways to organize and process information. Mastering these core programming concepts helps developers write clean, optimized code for real-world applications. Students and professionals interested in strengthening their Python data handling skills can explore Pandas Training, which covers practical data manipulation techniques using Python and Pandas.
Working with Python data structures such as lists, dictionaries, and DataFrames forms the foundation of many machine learning applications, where efficient data preprocessing directly impacts model performance. Understanding these programming techniques prepares learners to build intelligent applications involving classification, prediction, and analytics. Those looking to apply these skills to AI projects can further explore Machine Learning Projects for Final Year, featuring practical implementations of data preprocessing, predictive modeling, and intelligent systems.
ReplyDeleteReaders interested in expanding their Python programming knowledge can also refer to Python Training, which introduces essential Python libraries, frameworks, and concepts commonly used in data science, machine learning, and software development.
ReplyDelete