Module 1 · Python crash course · Lesson 3 of 66
Lists and indexing
Ordered collections you can grow, slice and nest.
In this lesson · about 12 minutes
- Create lists and add to them
- Index from 0 and slice with start:stop
- Reach inside nested lists
A list holds values in order inside square brackets. It can mix types, and even hold other lists: ['hi', 1, [1, 2]]. Lists are mutable: you can change them after creating them.
Positions (indexes) start at 0. my_list[0] is the first item, and my_list[-1] is the last.
A slice my_list[start:stop] takes items from start up to but not including stop. Leave a side empty to go to the edge: my_list[1:] is everything after the first item.
Nested lists
To reach inside a list within a list, chain the brackets. Work from the outside in, one bracket at a time.
Exercise
Dig out hello
Use indexing to grab the word 'hello' from this list. Your last line should be the expression that gives it.
Exercise
The middle of the week
Slice days to get ['Tue', 'Wed', 'Thu'].
Check your understanding0 of 2 correct
Quiz score: 0 of 2
Exercises solved: 0 of 2
Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.