How to Find Element In List In Python for Beginners

How to Find Element In List In Python for Beginners

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

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

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.

Learn More

Thanks for reading !

python

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

How To Compare Tesla and Ford Company By Using Magic Methods in Python

Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc.. You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like __init__, __call__, __str__ etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).

Python Programming: A Beginner’s Guide

Python is an interpreted, high-level, powerful general-purpose programming language. You may ask, Python’s a snake right? and Why is this programming language named after it?

Hire Python Developers

Are you looking for experienced, reliable, and qualified Python developers? If yes, you have reached the right place. At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, our full-stack Python development services...

Python any: How to Check If Element is Iterable or Not

Python any() function returns True if any element of an iterable is True otherwise any() function returns False. The syntax is any().