Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Problem No 8

Check membership in a set

Easy

8 minute session

Summary

Use a set to perform fast membership checks.

Problem statement

Write a function that checks whether a value is present in a collection. Convert the collection to a set (if it isn't already) and use Python's membership operator (in) to return True or False. This demonstrates using sets for fast membership checks.

Task

Determine whether a value exists in a collection by converting it to a set and using membership testing.

Examples

Value present in a list

Input

([1, 2, 3], 2)

Output

True

Explanation

2 is in the list, converting to a set keeps 2 and membership check returns True.

Input format

Two arguments: (collection, value). The collection can be a list or a set.

Output format

A boolean: True if value is found, False otherwise.

Constraints

Items are hashable. Comparison uses equality (==).

Samples

Sample input 0

([1, 2, 3], 2)

Sample output 0

True

Explanation 0

2 is present in the collection.

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

07:45 PM

Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.