Skip to main content

Description

Finds the common elements between two lists, returning a new list containing only the elements that appear in both input lists.

Syntax

Parameters

Returns

Type: list A new list containing only the elements that exist in both input lists. Preserves the order from the first list.

Examples

Example 1: Basic Intersection

Output:

Example 2: Finding Common Tags

Example 3: Finding Users with Shared Interests

Example 4: Filter by Allowed Values

Notes

  • Returns elements that exist in both lists
  • Preserves the order from the first list
  • If an element appears multiple times in list1, each occurrence is checked against list2
  • Efficient implementation using Set for fast lookup
  • Equivalent to mathematical set intersection operation

See Also

  • coll.union - Combine all unique elements from both lists
  • sim.jaccard - Calculate similarity coefficient using intersection

Frequently Asked Questions

It returns a list containing only the elements that appear in all of the input lists.
No. The result contains each common element only once, treating the inputs as sets.