Lesson guide
What this Python exercise practices
Check membership in a set is a beginner practice lesson that focuses on python practice, coding exercises, test feedback. It is designed to be solved in about 8 minutes with examples, starter code, and test feedback.
Prerequisites
- Python basics
- Variables
- Reading simple prompts
Difficulty and time
- Level
- Beginner
- Estimated time
- 8 minutes
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.
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.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.