How to Parse Bullet Points in Python
Extract bullet point items from raw text by splitting lines and filtering those that start with '- ' or '* '.
Python code
26 linesdef parse_bullet_points(text):
"""Extract bullet point items from raw text."""
lines = text.splitlines()
items = []
for line in lines:
stripped = line.strip()
if stripped.startswith("- ") or stripped.startswith("* "):
item = stripped[2:]
if item:
items.append(item)
return items
if __name__ == "__main__":
sample_text = """Shopping List
- Apples
* Bananas
- Oranges
* Grapes (indented)
Regular line, not a bullet
- """
result = parse_bullet_points(sample_text)
print(f"Found {len(result)} items: {result}")
Output
Found 4 items: ['Apples', 'Bananas', 'Oranges', 'Grapes (indented)']
How it works
The function uses splitlines() to get each line from the input text, then iterates over them with a for loop. After stripping whitespace, it checks if the line starts with a bullet marker (- or *). If so, it removes the first two characters (the marker and a space) and appends the item to the list. The conditional check if item filters out empty bullets, like a trailing - with only spaces. This pattern is efficient for simple text parsing without external libraries.
Common mistakes
- Forgetting to handle indented bullets by using `strip()` before checking
- Not removing the marker space, leaving extra whitespace in items
- Assuming only one bullet style; missing lines that start with `* ` or other markers
Variations
- Use a list comprehension with a startswith condition to build the list in one line
- Add support for numbered lists by checking `re.match(r'\d+\.', line)`
Real-world use cases
- Parsing markdown-style bullet lists from user notes to convert into structured tasks.
- Extracting feature requests from plain-text ticket descriptions for workflow automation.
- Reading configuration files that use bullet markers to define list-valued settings.
Sponsored
More from Lists & loops
- Check if List is Sorted Ascending in Python easy
- Compare Two Lists in Python: Common, Only in First, Only in Second easy
- Convert a List of Integers to a Comma-Separated String in Python easy
- Enumerate a Python List with a Custom Start Index easy
- Extract Data by Type from a List in Python: Numbers and Strings easy
- Find All Occurrences of an Item in a Python List easy
Keep learning
Related tutorials and quizzes for this topic.