How to Check if an Element is in a Python List

Python has a reputation as a programming language that does things a little differently than one might expect. Lists are one of the best examples of this philosophy. In theory, Python program lists are quite similar to arrays in other languages. But in practice, a list’s versatility makes it extremely powerful. For example, consider how easily we can check for a specific element inside a Python list.

In this check if element in list Python tutorial example, we’ll imagine that we’re entering a list of animals. But oh no, the “m” key was broken! We need to search for instances where we wrote agpies instead of Magpies.

animalList = ['Crows', 'agpies', 'Ravens']
if 'agpies' in animalList:     
print("agpies found in list")

For the sake of the Python tutorial example, we begin with a given list containing the mistyped list value. Next, we perform a simple search for the ‘agpies’ string within animalList. The original list element is found so we’ll see “agpies found in list” printed onto the screen. This is by far the easiest way to search for a given element within a Python list. But there are times when we need to go about things a little differently. Let’s consider an example where we want to use the count function within our Python code.

We’re putting together an original list of birds in the corvid family. But it appears that we may have made a mistake. We might have incorrectly added hummingbirds to the input list. How would we check to find out if that’s the case by using a Python program? We’ve seen one method of doing so. But in the following example, we’ll use something known as count.

birdList = ['Crows', 'Magpies', 'Ravens', 'Jackdaws', 'Rooks', 'Treepies', 'Hummingbirds']
searchItem = 'Hummingbirds'
if birdList.count(searchItem) > 0:     
print(searchItem," are in the list")
else:
    print(searchItem," aren't in the list")

The count function is an extremely useful tool when working with lists. When we call count we can pass a string element, integer or almost anything else. The function will search through the item for whatever parameter was passed during the initial call. In this case we looked for the “Hummingbirds” string. The count function will return the number of instances found within the input list.

We only need to know whether the “Hummingbirds” string exists within the true list. So we just check to see if the total number of iteration instances is greater than zero in the output. We then use a simple if/else to respond appropriately depending on whether “Hummingbirds” are found in the given list output.

But how would we go about detecting elements within a list and then manipulating those items if they’re found? We can do so quite easily with list comprehension. This gives us the ability to use programming logic, including a loop method, when creating lists. In the following example imagine that we’ve created a large list of birds. But we suspect that there might be duplicates within it. We want to find and remove those duplicate elements so that only the unique values remain. Duplicate values cause issues, so we would have to do this a different way if those duplicate values were not removed from the new list

birdList = ['Crows', 'Magpies', 'Ravens', 'Jackdaws', 'Rooks', 'Treepies', 'Hummingbirds','Crows','Crows']
uniqueList = []
[uniqueList.append(element) for element in birdList if element not in uniqueList]
print(uniqueList)

We begin by creating our example list and a new empty list called uniqueList. The uniqueList variable is created without any contents. However, we’ll begin adding items through an append function. We essentially loop through the birdList and check whether a given element exists before adding it. If the given string element item isn’t in the new list, then we pass it as an input string to append. Finally, we print the uniqueList variable to screen. Keep in mind that we could also assign the contents of uniqueList back to the original birdList if desired.

It’s also important to note that the order of our data structure is still intact. The method used here doesn’t just retain the unique items. It also retains the order in which all items appear within the list.

How to Check if an Element is in a Python List

Leave a Reply

Your email address will not be published.

Scroll to top