โ† Back to Python Course
๐Ÿ“ฆ Lists & Dataยท25 minยทIntermediate
๐Ÿ”ง

List Operations

Add, remove, sort, and slice

Level up your lists! Learn to add items, remove items, sort them, and slice out pieces.

๐Ÿ“–

What we are learning

Lists are not static โ€” you can change them! Add items with append(), remove items with remove(), sort them with sort(), and extract pieces with slicing. These operations make lists incredibly powerful.

๐Ÿ’ก Think of it like this:

Think of a list like a notebook. You can add new pages (append), tear out pages you do not want (remove), rearrange the pages (sort), and copy a section of pages (slice).

๐ŸŒŸ

Why it matters

Real programs constantly modify lists. A game adds items to your inventory, removes used ones, and sorts them by type. A to-do app adds tasks, removes completed ones, and sorts by priority.

Where you see this in real life:

  • โœ“To-do apps add, remove, and reorder tasks
  • โœ“Email apps sort messages by date, sender, or subject
  • โœ“Shopping carts add and remove items as you shop
๐ŸŽฏ

By the end, you can:

  • 1.Add items with append() and insert()
  • 2.Remove items with remove() and pop()
  • 3.Sort lists with sort() and reverse()
  • 4.Slice lists with [start:stop]
๐Ÿ”ค

New words

appendinsertremovepopsortslice
๐Ÿ
If you had a list of your friends and someone new joined your class, how would you add them? What if someone left?
๐Ÿค“

Fun Fact!

Python lists can hold any type of data โ€” even other lists! You can have a list of lists, like a grid or a spreadsheet.

๐Ÿ”ง List Operations

Lists are not fixed โ€” you can change them! Add items, remove items, sort them, and slice out pieces. These operations make lists incredibly powerful.

โž• Adding Items

Use append()to add an item to the end of a list.

๐ŸAdd items with append() and insert()
main.py
โ— Output
Click โ–ถ Run to see the output!

โž– Removing Items

Use remove()to remove a specific item, or pop()to remove by index.

๐ŸRemove items with remove() and pop()
main.py
โ— Output
Click โ–ถ Run to see the output!

๐Ÿ”€ Sorting Lists

Use sort()to put items in order, and reverse()to flip the order.

๐ŸSort and reverse lists
main.py
โ— Output
Click โ–ถ Run to see the output!

โœ‚๏ธ Slicing Lists

You can cut out a piece of a list using slicing:list[start:stop]

๐ŸSlice lists with [start:stop]
main.py
โ— Output
Click โ–ถ Run to see the output!
๐Ÿ
Try this: create a list of 6 numbers. Sort them, then print the top 3 (the last 3 after sorting)!
๐Ÿง 

Quick Quiz!

+10 XP

What does the append() method do?

๐Ÿ
Finished the lesson? Click the button to mark it complete and earn XP!