Check if an Item Exists Practice Problem
This data science coding problem helps you practice Lists & Basic Data Structures, check if an item exists, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Lists & Basic Data Structures.
- Problem ID: 69
- Problem key: 69-check-if-an-item-exists
- URL: https://datacrack.app/solve/69-check-if-an-item-exists
- Difficulty: easy
- Topic: Lists & Basic Data Structures
- Module: Python Fundamentals
Problem Statement
# 🧩 Check if an Item Exists
---
### 🎯 Goal
Before processing data, you often need to check whether a value is present in a collection. Python's `in` keyword is the most **Pythonic** way to do this — it works on lists, strings, dictionaries, sets, and more.
---
### 🔍 The `in` Operator
```python
fruits = ["apple", "banana", "cherry"]
"banana" in fruits # True
"mango" in fruits # False
"mango" not in fruits # True
```
| Expression | Result |
|:-----------|:-------|
| `x in lst` | `True` if `x` is found anywhere in `lst` |
| `x not in lst` | `True` if `x` is NOT in `lst` |
> 💡 For lists, `in` performs a **linear scan** (O(n)). For sets and dicts, it's O(1).
---
### 💻 Task
Implement `item_exists(lst, item)` that returns `True` if `item` is in the list, `False` otherwise.
---
### 📥 Input
- `lst`: list — the list to search
- `item`: the value to look for
### 📤 Output
- `bool`: `True` if found, `False` if not
---
### 🧩 Starter Code
```python
def item_exists(lst, item):
"""
Check whether item exists in lst.
Args:
lst (list): The list to search
item: The value to look for
Returns:
bool: True if item is in lst, False otherwise
"""
# 🧠 TODO: Use the 'in' operator to check membership
pass
```
---
### 💡 Example
```python
item_exists([1, 2, 3, 4, 5], 3)
# Expected: True
item_exists([1, 2, 3, 4, 5], 6)
# Expected: False
```
---
### 🔑 Key Concepts
- `in` is the Pythonic way to check membership
- Works on lists, tuples, strings, sets, and dictionary keys
- Returns a `bool` (`True` / `False`)
- For large datasets, consider using a `set` for O(1) lookups instead of a listStarter Code
def item_exists(lst, item):
"""
Check whether item exists in lst.
Args:
lst (list): The list to search
item: The value to look for
Returns:
bool: True if item is in lst, False otherwise
"""
# 🧠 TODO: Use the 'in' operator to check membership
pass