Add, remove, sort, and slice
Level up your lists! Learn to add items, remove items, sort them, and slice out pieces.
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).
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:
Python lists can hold any type of data โ even other lists! You can have a list of lists, like a grid or a spreadsheet.
Lists are not fixed โ you can change them! Add items, remove items, sort them, and slice out pieces. These operations make lists incredibly powerful.
Use append()to add an item to the end of a list.
Click โถ Run to see the output!Use remove()to remove a specific item, or pop()to remove by index.
Click โถ Run to see the output!Use sort()to put items in order, and reverse()to flip the order.
Click โถ Run to see the output!You can cut out a piece of a list using slicing:list[start:stop]
Click โถ Run to see the output!What does the append() method do?