Python Lists

When your collection of items needs to constantly adapt—growing, shrinking, and reordering—what's the most flexible Python data structure for the job? Python lists are ordered, mutable collections that excel at handling dynamic data.

The Power of Flexibility: What Lists Are and How to Create Them

A Python list is a fundamental data structure designed to store an ordered sequence of items. Unlike some other data structures, lists are mutable, meaning you can change their contents—add, remove, or modify elements—after they have been created. This dynamic nature makes them incredibly versatile for tasks where data collections frequently change. Lists can also hold items of different data types, offering great flexibility.

Python List
An ordered, mutable collection of items that can hold elements of different data types, enclosed in square brackets.
Example: A shopping list: ['apples', 'milk', 'bread']
pythonCreating Lists and Accessing Elements

Modifying Lists: Adding, Removing, and Changing Elements

The mutability of lists is a core feature that distinguishes them from other data structures like tuples. This means you can alter a list's contents directly after it has been created. You can add new items, remove existing ones, or change the value of an element at a specific position. Python provides several built-in methods and keywords to perform these modifications efficiently.

pythonDemonstrating List Modification Methods
Try It Yourself
Starting with the inventory list, add 'keyboard' to the end, then insert 'mouse' at the second position (index 1), and finally remove 'monitor'. Print the list after each operation.
python

Understanding List Aliasing: A Common Pitfall

When you assign one list variable to another, like list_b = list_a, you might expect list_b to become an independent copy of list_a. However, this is not what happens. Instead, both list_a and list_b become aliases; they both refer to the exact same list object in memory. Think of it like two different labels pointing to the same physical box. If you change the contents of the box using one label, the changes are visible when you look at the box using the other label.

pythonIllustrating List Aliasing
Check Your Understanding
If list_A = [1, 2, 3] and list_B = list_A, what will list_A be after list_B.append(4)?
Creating True List Copies

To create an independent copy of a list, preventing aliasing issues, use one of these methods:

  • Slicing: new_list = original_list[:] (most common and concise)
  • list() constructor: new_list = list(original_list)
  • copy module: import copy; new_list = copy.copy(original_list) (for shallow copies) or copy.deepcopy(original_list) (for nested lists)

Essential List Operations and When to Choose a List

pythonCommon Built-in Functions and List Methods
Comparing list.sort() vs. sorted()
Featurelist.sort() Methodsorted() Function
MutabilityModifies the list in-placeReturns a new sorted list, leaves original unchanged
Return ValueReturns NoneReturns the new sorted list
ApplicabilityOnly for listsWorks on any iterable (lists, tuples, strings, etc.)
Use CaseWhen you need to sort a list and don't need the original orderWhen you need a sorted version but want to preserve the original iterable
Choose list.sort() for in-place modification and sorted() for a new sorted iterable.
Lists vs. Tuples: Choosing the Right Data Structure
FeaturePython ListPython Tuple
SyntaxUses square brackets []Uses parentheses ()
MutabilityMutable (can be changed after creation)Immutable (cannot be changed after creation)
OrderOrderedOrdered
PerformanceSlightly slower for access/iteration due to mutability overheadGenerally faster for access/iteration due to immutability
Typical Use CasesDynamic collections, stacks, queues, modifying data frequentlyFixed collections of related items (e.g., coordinates, database records), dictionary keys
Lists are for dynamic, changeable data, while tuples are for fixed, unchangeable sequences.
Key Takeaways
  • Python lists are ordered and mutable collections, allowing elements to be added, removed, or changed after creation.

  • Elements are accessed using zero-based indexing and slicing, supporting both positive and negative indices.

  • Common modification methods include append(), insert(), extend(), remove(), pop(), del, and direct item assignment.

  • Assigning one list to another creates an alias, where both variables refer to the same object in memory; changes via one variable affect the other.

  • To create an independent copy of a list, use slicing ([:]), the list() constructor, or the copy module.

  • Built-in functions like len(), min(), max(), sum(), and methods like sort() and reverse() offer powerful list manipulation.

  • Lists are the ideal choice when your data collection needs to be dynamic, frequently growing, shrinking, or reordering its contents.

← All lessons in Data Structures: Lists & Tuples

Ready to keep this from fading?

Bitelrn turns lessons like this into a full course — quizzes, a knowledge map, and spaced review.

Get started free