List Methods and Slicing
Lists in Python are powerful - they let you store multiple items and also change, extend, and extract parts of them.
Two key skills every analyst should know when working with lists are:
- Using list methods to update content
- Using slicing to access sections of a list
Common List Methods
List methods make it easy to modify the contents of a list. Some useful beginner-friendly methods are:
.append(): add an item to the end.insert(): add an item at a specific index.remove(): delete the first matching item.pop(): remove an item by index.sort(): sorts the list in place
List Slicing
Slicing lets you extract parts of a list by specifying a start and stop index.
List Slicing
numbers[1:4] # Gets items from index 1 to 3 (not including 4)
You can also use:
[:]: copy the whole list[-3:]: get the last three items[::2]: get every second item
These techniques make it easier to work with subsets of data.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.