October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Python List Membership: How to Check for Any Shared Element

Use any(item in second_list for item in first_list) for a Boolean check. Learn when set lookup is useful and how to return common values without losing list order.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use any(item in second_list for item in first_list) to get a Boolean indicating whether two lists share at least one value. Python stops at the first match; if there is no match—or the first list is empty—the expression returns False.

Check whether the lists share at least one value

For a one-off check or modest lists, combine Python’s any() function with the in membership operator:

As an Amazon Associate I earn from qualifying purchases.

first_list = ["apple", "pear", "plum"]
second_list = ["orange", "pear"]

has_common_item = any(item in second_list for item in first_list)
print(has_common_item)  # True

The expression tests each item from first_list against second_list. any() returns True as soon as one membership test succeeds, so it does not need to check the remaining items. If every test fails, it returns False. An empty first_list also produces False, because there is no item to match. Python defines in as a membership test and its Boolean operations short-circuit. Python Language Reference: Membership test operations.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose list membership or a set for repeated checks

Use lists for a simple, one-time check

The direct expression is usually the clearest choice when the lists are small or you only need to check once. A list membership test may scan through the searched list, however, so doing it repeatedly can add up as the inputs grow.

Build a set when you will reuse the searched values

If you will check many items against the same list, convert that list to a set once and reuse it:

second_set = set(second_list)
has_common_item = any(item in second_set for item in first_list)

The CPython time-complexity reference classifies list membership as O(n) and set membership as O(1) on average, with O(n) as the worst case for set membership. Creating the set also takes time and memory proportional to the input. It is therefore more likely to be useful for larger inputs or repeated lookups than for a single check on small lists. These complexity figures describe CPython, not a guarantee for every Python implementation. Python Wiki: Time Complexity.

Return matching values instead of a Boolean

If you need the common values, choose the approach based on whether order and duplicates matter.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Get unique common values

Set intersection returns the shared values as an unordered set, with duplicates removed:

common_items = set(first_list) & set(second_list)
has_common_item = bool(common_items)

The conversion requires every element to be hashable. A set is useful when unique matches are the goal, but it does not preserve list order or duplicate counts.

Keep the first list’s order and repeated matches

Filter the first list to retain each item that appears in the second:

matching_items = [item for item in first_list if item in second_list]

This preserves the order and duplicate occurrences from first_list. It uses list membership for each item; if performance matters and the searched values are hashable, use a set for membership while retaining the original list for the output:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
second_set = set(second_list)
matching_items = [item for item in first_list if item in second_set]

Understand what counts as a match

  • Equality, not just the same object: For built-in containers, membership tests compare by identity or equality. A value can match an equal value even when it is a different object. Python Language Reference: Membership test operations.
  • Dictionary membership checks keys: If the searched object is a dictionary, item in dictionary tests whether item is a key, not whether it is one of the dictionary’s values. Use item in dictionary.values() when values are what you mean to search.
  • Sets need hashable elements: Lists and dictionaries cannot be placed directly in a set. Keep list membership, or compare a hashable representation such as a tuple or an ID field.
  • Match records by the intended field: If two records should count as the same when their id fields match, compare those fields explicitly rather than comparing the entire records.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Performance at a glance

Method Useful when Cost and behavior
any(x in second_list for x in first_list) Inputs are small or the check is one-off In CPython, each list membership can scan O(n) values; uses ordinary membership semantics.
Build second_set, then test membership You will reuse the searched values or have larger inputs Set construction costs time and memory; membership is O(1) average and O(n) worst case in the cited CPython reference. Requires hashable elements and removes duplicates from the set.
set(first_list) & set(second_list) You need unique common values Builds both sets and a result; output is unordered and unique.

O(n) describes growth with the number of elements, not a measured runtime. The complexity classifications above come from CPython documentation and should not be treated as a benchmark or as a promise about every Python implementation. Python Wiki: Time Complexity.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.