Lists
Introduction to Lists
Lists are one of Python's most versatile data structures. They allow you to store multiple items in a single variable, making it easy to work with collections of data. Lists are ordered, changeable (mutable), and can contain duplicate values.
Lists are perfect for storing sequences of items like shopping lists, scores, names, or any collection of related data that you need to access, modify, or iterate over.
Creating Lists
Lists are created using square brackets [] with items separated by commas. Lists can contain any data type, including mixed types:
Accessing List Items
You can access list items by their index (position). Python uses zero-based indexing, meaning the first item is at index 0:
Modifying Lists
Lists are mutable, meaning you can change their contents after creation:
List Slicing
Slicing allows you to get a subset of a list by specifying a range of indices:
Iterating Over Lists
You can loop through lists using for loops:
Checking if Item Exists
Use the in keyword to check if an item exists in a list:
Best Practices
✅ List Usage Tips
• Use lists for ordered collections of items
• Lists can contain any data type, including other lists
• Remember: indices start at 0, not 1
• Use negative indices to access from the end
• Lists are mutable - you can modify them
💡 Important Notes
• Lists use square brackets []
• Items are separated by commas
• Lists preserve order (items stay in the order you add them)
• Lists can contain duplicate values