๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท
๐Ÿ Python

Arrays / Lists

๐Ÿ“š What are Lists? A list is an ordered collection that holds multiple values in a single variable. Instead of creating score1, score2, score3 separately you write scores = [85, 92, 78] and handle all three at once. Lists keep their order, allow duplicates, and can hold any type of value.

8 min 10 XP Lesson 7 of 21
Arrays / Lists
๐ŸŒ

Appy Saysโ€ฆ

You can't make a music app with a separate variable for every song. You need a list. Lists are how you handle collections of anything โ€” playlists, leaderboards, shopping carts, user feeds โ€” all built on lists.

๐Ÿ“–

What is a List?

A list stores multiple values in a single variable, in order. Each item has an index (position number), starting at 0. You can add, remove, change, and loop through items.

  • โ€ขCreate: fruits = ["apple", "banana", "cherry"]
  • โ€ขAccess by index: fruits[0] gives "apple" (first item)
  • โ€ขNegative index: fruits[-1] gives the last item
  • โ€ขChange: fruits[1] = "mango" replaces the second item
  • โ€ขAdd to end: fruits.append("date")
  • โ€ขLength: len(fruits) gives 3
๐ŸŽฎ

Think of it like a Spotify playlist

A playlist is a list of songs in order. Song at position 0 plays first. You can add a new song at the end (append), remove one you don't like (remove), skip to any position by index, and loop through all of them to play each one. A Python list works exactly like this.

โš™๏ธ

Key List Operations

  • โ€ขlist.append(item) โ€” add to the end
  • โ€ขlist.insert(index, item) โ€” add at a specific position
  • โ€ขlist.remove(item) โ€” remove first occurrence of that value
  • โ€ขlist.pop() โ€” remove and return the last item (or pop(index) for a specific one)
  • โ€ขlist[start:end] โ€” slice: get a sub-list from start up to (not including) end
  • โ€ขitem in list โ€” check if an item is in the list (True/False)
  • โ€ขlist.sort() โ€” sort in place; sorted(list) returns a new sorted list
๐ŸŒ

Real-World Examples

  • โ€ขA game leaderboard: scores = [9800, 7200, 6400, 5100]
  • โ€ขA shopping cart: cart = ["shoes", "jacket"] โ€” cart.append("bag") when adding
  • โ€ขA TikTok feed: each video is an item in a list the algorithm fills
  • โ€ขChess moves: moves = [("e2", "e4"), ("e7", "e5")] โ€” a list of pairs
  • โ€ขContacts app: contacts = [{"name": "Alex", "phone": "07..."}, ...]
๐Ÿ’ก

Key Facts

  • โ€ขLists can hold mixed types: [1, "hello", True, 3.14]
  • โ€ขLists can hold other lists (nested): [[1,2],[3,4]] โ€” useful for grids
  • โ€ขSlicing: nums[1:4] returns items at index 1, 2, 3 (not 4)
  • โ€ขnums[::-1] reverses the list โ€” a useful trick
  • โ€ขLists are mutable (changeable) โ€” unlike strings or tuples
โš ๏ธ

Watch Out: Index Out of Range

Accessing an index that doesn't exist gives an IndexError. If your list has 3 items (indices 0, 1, 2), trying list[3] crashes. Always check len() if you're not sure, or use negative indices for the last few items.

๐Ÿ“Œ

Remember

Indexing starts at 0, not 1. The first item is list[0], the second is list[1]. This trips up almost every beginner at least once.

โœ…

What You Learned

  • โ€ขLists store multiple values in order in one variable
  • โ€ขIndex starts at 0 โ€” first item is list[0]
  • โ€ขAdd with append(), remove with remove() or pop()
  • โ€ขLoop through with for item in list:
  • โ€ขSlice with list[start:end] to get a portion
  • โ€ขCheck membership with item in list

Key Facts

  • โ†’Lists can hold mixed types: [1, "hello", True, 3.14]
  • โ†’Lists can hold other lists (nested): [[1,2],[3,4]] โ€” useful for grids
  • โ†’Slicing: nums[1:4] returns items at index 1, 2, 3 (not 4)
  • โ†’nums[::-1] reverses the list โ€” a useful trick
  • โ†’Lists are mutable (changeable) โ€” unlike strings or tuples

Real-World Examples

โ€ข A game leaderboard: <code>scores = [9800, 7200, 6400, 5100]</code> โ€ข A shopping cart: <code>cart = ["shoes", "jacket"]</code> โ€” <code>cart.append("bag")</code> when adding โ€ข A TikTok feed: each video is an item in a list the algorithm fills โ€ข Chess moves: <code>moves = [("e2", "e4"), ("e7", "e5")]</code> โ€” a list of pairs โ€ข Contacts app: <code>contacts = [{"name": "Alex", "phone": "07..."}, ...]</code>

Remember

Indexing starts at 0, not 1. The first item is list[0], the second is list[1]. This trips up almost every beginner at least once.

Quick Quiz

1 / 2

What is the first index in a list?