Get element from list Python

Python

Python Find in List: How to Find Element in List

By Krunal Last updated Oct 16, 2020 0

Python list is an essential container as it stores elements of all the datatypes as a collection. Knowledge of certain list operations is necessary for day-day programming.

Python find in list

To find an element in the Python list, use one of the following approaches.

  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

To find an element in the list, use the Python list index[] method, The index[] method searches an item 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; forexample,

  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 doesnt 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 calleda searchthat 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 the list using the in operator

To check if an element exists in the list, use Python in operator.

Syntax

element in list

It will return True if an element exists in the 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

To find all elements in a sequence that meet a specific condition, use the list comprehension or generator expressions.

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, 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

Conclusion

Python list can contain different data types like integer, string, boolean, etc. Sometimes, it requires to search particular elements in the list. The items can be searched in the python list in various ways. We have seen the ways like search element in the list by index, linear search on the list,

That is it for the Python find in list example.

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

Python list is an essential container as it stores elements of all the datatypes as a collection. Knowledge of certain list operations is necessary for day-day programming.

Python find in list

To find an element in the Python list, use one of the following approaches.

  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

To find an element in the list, use the Python list index[] method, The index[] method searches an item 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; forexample,

  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 doesnt 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 calleda searchthat 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 the list using the in operator

To check if an element exists in the list, use Python in operator.

Syntax

element in list

It will return True if an element exists in the 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

To find all elements in a sequence that meet a specific condition, use the list comprehension or generator expressions.

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, 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

Conclusion

Python list can contain different data types like integer, string, boolean, etc. Sometimes, it requires to search particular elements in the list. The items can be searched in the python list in various ways. We have seen the ways like search element in the list by index, linear search on the list,

That is it for the Python find in list example.

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

Video liên quan

Chủ Đề