Check if an Element Exists in a List in Python

  • Post category:List

Checking if an element exists in a list is a common task in Python programming. In this blog post, we’ll explore different code examples that demonstrate how to efficiently determine if an element is present in a list.

Example 1:

fruits = ['apple', 'banana', 'kiwi']
search_fruit = 'banana'
found = search_fruit in fruits

Output: True

Example 2:

numbers = [1, 2, 3, 4, 5]
search_number = 6
found = any(search_number == num for num in numbers)

Output: False