Lists in Python
Store and manipulate ordered collections of data — the most-used Python data structure
A list is an ordered collection of items in Python. Lists are the most common data structure you will work with as a data analyst — column names, category values, file paths, and filter conditions are all stored as lists.
Lists are mutable (you can change them) and can hold any mix of data types. They are indexed starting at 0.
Syntax
my_list = [item1, item2, item3] my_list[0] # first item (index 0) my_list[-1] # last item my_list[1:3] # slice: items at index 1 and 2
Examples
Key Points
- ✓Index starts at 0: list[0] is the first item
- ✓Negative index: list[-1] is the last item, list[-2] is second-to-last
- ✓Slicing: list[start:end] — end index is exclusive
- ✓append() adds one item; extend() adds another list; insert() adds at a position
- ✓List comprehension: [expr for item in list if condition] — fast and readable
Practice Question
Given cities = ["Delhi", "Noida", "Gurgaon"], what does cities[-1] return?