How can you find a list of silver and gold members using square brackets in Python?
💡 Model Answer
In Python, you can filter a list of objects by membership status using a list comprehension or the filter() function. For example, if you have a list members where each element has a membership_status attribute, you can get all silver and gold members with: silver_and_gold = [m for m in members if m.membership_status in (MembershipStatus.SILVER, MembershipStatus.GOLD)]. The square brackets denote a list literal, and the in operator checks membership in the tuple of allowed statuses. This returns a new list containing only the members that match the criteria. If you prefer a more functional style, you could write: silver_and_gold = list(filter(lambda m: m.membership_status in (MembershipStatus.SILVER, MembershipStatus.GOLD), members)). Both approaches are O(n) in time and O(k) in space, where k is the number of matching members. Using a tuple for the status list is efficient because tuples are immutable and have a small memory footprint, making the in check fast.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI — Starts at ₹500