AppDividend
Latest Code Tutorials

How To Find Element In List In Python

0

In this tutorial, we will see how to search an element in list in Python. The list is an essential container in python as if stores elements of all the datatypes as a collection. Knowledge of certain list operations is necessary for day-day programming.

There are so many ways that you can find an element in the list, some of which are the following.

  1. Find an element in the list by index in Python.
  2. Python Linear search on the list. 

Find Element In List By Index In Python

Content Overview

Python list index() method searches an element in the list and returns its index.

Python index() method finds the given element in the list and returns its position. If the same element is present more than once, the method returns the index of the first occurrence of the element.

The index in Python starts from 0, not 1.

So through an index, we can find the position of an element in the list.

See the following code example.

# app.py

streaming = ['netflix', 'hulu', 'disney+', 'appletv+']

index = streaming.index('disney+')
print('The index of disney+ is:', index)

Output

➜  pyt python3 app.py
The index of disney+ is: 2
➜  pyt

The index() method takes a single argument, which is the element, and it returns its position in the list.

Python search on the list

It is the straightforward approach is to do a linear search, for example,

  1. Start from the leftmost item of the list and one by one compare x with each item of the list.
  2. If x matches with an item, return True.
  3. If x doesn’t match with any of the items, return False.

See the following code.

# app.py

def search(list, platform):
    for i in range(len(list)):
        if list[i] == platform:
            return True
    return False


streaming = ['netflix', 'hulu', 'disney+', 'appletv+']
platform = 'netflix'

if search(streaming, platform):
    print("Platform is found")
else:
    print("Platform does not found")

In the above code, we have first created a user-defined function called search that accepts two arguments.

The first argument is our list in which we need to find the item, and the second parameter is the platform, which is the string we need to search in the list.

So, we are looping through a list and compare each element of the list to the platform argument.

If both are matched, then the element is found; otherwise, it is not.

Output

➜  pyt python3 app.py
Platform is found
➜  pyt

Check if the item exists in list using “in” operator

How to check if the element exists in the list using Python in operator.

See the syntax of Python in operator.

element in list

It will return True if element exists in list else return False.

See the following code.

# app.py

streaming = ['netflix', 'hulu', 'disney+', 'appletv+']
platform = 'hulu'
if platform in streaming:
  print('Hulu is in the streaming service business')
else:
  print('It does not include')

Output

➜  pyt python3 app.py
Hulu is in the streaming service business
➜  pyt

Filtering a collection in Python

That is, finding all elements in a sequence that meet a specific condition. You can use list the comprehension or generator expressions for that.

See the following code example.

# app.py

streaming = ['netflix', 'hulu', 'disney+', 'appletv+']
platform = 'hulu'
result = any(len(elem) == 8 for elem in streaming)

if result:
    print("Yes, string with length 8 is found")
else:
    print('Not found')

In the above code, we are searching for an element whose string length is 8. If found, then it will print “Yes, string with length 8 is found” otherwise not found.

Output

➜  pyt python3 app.py
Yes, string with length 8 is found
➜  pyt

Finally, we have seen the ways to find element in list In Python.

Related Posts

How To Access Characters In String By Index In Python

How To Convert Python List To JSON

How To Convert Python Dictionary To String

How To Convert Python List To Dictionary

How To Convert Python String To List And List To String

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.