Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Implement __contains__ for Membership Checks

Create a case-insensitive string container by implementing __contains__.

Python practice16 minMagic Methods & Operator OverloadingIntermediateLast updated April 11, 2026

Problem statement

Implement the __contains__ method for the CaseInsensitiveSet class. The class stores string items but membership checks using the in operator should be case-insensitive. Requirements: - When checking membership with 'item in collection', if item is a string, the check should be case-insensitive (e.g., 'Apple' and 'apple' should be considered equal). - If the probe is not a string, __contains__ should return False. - The class should preserve the original items (keeping them as provided) but internal membership should be efficient. Complete the TODO in __contains__. A helper function contains_in_collection(items, probe) is provided and used by tests.

Task

Implement __contains__ so that the container supports 'in' membership checks that ignore string case and return False for non-string probes.

Examples

Case-insensitive membership

Input

contains_in_collection(['Apple', 'banana'], 'APPLE')

Output

True

'APPLE' matches 'Apple' ignoring case, so membership returns True.

Input format

A single function call: contains_in_collection(list_of_strings, probe).

Output format

Boolean True or False indicating membership.

Constraints

- Do not modify the probe value permanently. - Only strings are considered for membership; return False for other types.

Samples

Sample 1

Input

contains_in_collection(['Hello', 'World'], 'world')

Output

True

Membership check is case-insensitive.