Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Search Elements in a Matrix - Python
Next article icon

Finding an item in an array inPython can be done using several different methods depending on the situation. Here are a few of the most common ways to find an item in aPython array.

Using the in Operator

in operator is one of the most straightforward ways to check if an item exists in an array. It returns True if the item is present and False if it's not.

Python
importarray# Create an array of integersarr=array.array('i',[1,2,3,4,5])# Check if an item exists in the arrayval=3ifvalinarr:print("Item found")else:print(f"Not found")

Output
Item found

Let's take a look at methods of finding an item in an array:

Using index() Method

If we want to find the index of an item in the array, we can use the index() method. This method returns the index of the first occurrence of the item in the array. If the item is not found, it raises a ValueError.

Python
importarray# Create an array of integersarr=array.array('i',[1,2,3,4,5])# Find the index of an itemtry:val=4idx=arr.index(val)print(f"found at index{idx}.")exceptValueError:print("not found")

Output
found at index 3.

Note: index() is useful if you also want the index of the item, but it raises an error if the item is not found.

Using a Loop for Custom Searching

If we need to perform more complex search operations such as checking for multiple occurrences or applying custom logic, we can loop through the array manually.

Python
importarray# Create an array of integersarr=array.array('i',[1,2,3,4,5,3])# Find all occurrences of an itemval=3idx=[]foriinrange(len(arr)):ifarr[i]==val:idx.append(i)ifidx:print(f"found at indices:{idx}.")else:print("not found")

Output
found at indices: [2, 5].

This approach can be modified to handle more complex search requirements (e.g., partial matches, case-insensitive searches).

Using filter() Function for Advanced Filtering

If you want to filter items based on certain criteria, you can usefilter() function in combination with a lambda function.

Python
importarray# Create an array of integersarr=array.array('i',[1,2,3,4,5,6,7])# Find all even numbers using filterli=list(filter(lambdax:x%2==0,arr))print(li)

Output
[2, 4, 6]

The filter() function filters out all elements in the array that satisfy the given condition (in this case, even numbers).



Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp