Use list and tuple
Python is an entry-friendly programming language, and container type (a structure that can hold multiple data) is the first lesson that cannot be avoided. list (list) and tuple (tuple) are the most commonly used "ordered sequence dual containers" - they can store any type of elements, and both support index slicing, but one "changes it if you want", and the other "once created, it cannot be moved (the structure is not allowed to be moved, the content depends on the element)". Today I will explain all the usage, differences and best practices of these two~
List
List is the most basic variable ordered sequence in Python, which is suitable for storing data that needs to be dynamically added, deleted, and modified.
Create a list
Square brackets for lists[]Wrap elements separated by commas:
Access list elements
There are two core rules for using index (subscript) to locate elements:
⚠️ Attention! Newbies often fall into pitfalls:
- Index always starts from 0 (not 1);
- Negative indexes work backwards from -1 (the last element), for example -2 is the second to last element;
- Index out of bounds will be thrown directly
IndexError: list index out of range。
List common operations
1. Get the length of the list
Using Python’s built-in universal length functionlen():
2. Modify elements
Just assign the value directly through the index:
3. Add elements
There are three common methods, each with its own applicable scenarios:
4. Delete elements
Three common methods:
Multidimensional lists (nested lists)
Lists can be nested within lists to form two-dimensional/three-dimensional data structures (such as simulation matrices and grids):
💡 Practical tips: Want to quickly get a column of a two-dimensional list? available
zip(*matrix)Transpose first (the result is an iterator of the tuple, and to convert it into a list, addlist()), we will learn more about iterators later~
List slicing (super useful function!)
Slicing can access a subset of the list at once. The complete syntax islist[start:end:step], all three parameters are optional**!
start: Starting index (default 0, inclusive)end:End index (default to the end, not included)step: step size (default 1, negative number means reverse direction)
Tuple
Tuples are pseudo-immutable ordered sequences in Python, suitable for storing fixed data that should not be modified.
Create tuple
Tuples with parentheses()Wrapping elements (sometimes can be omitted, such as function return, tuple unpacking), but there is a super important rule:
⚠️ **Super warning! ** Create a tuple with only one element, must add a comma! Otherwise, Python will treat it as an ordinary bracket operator (priority control), creating a variable with a single value!
Access tuple elements
It is exactly the same as a list - both indexing and slicing can be used! But because it is immutable, elements cannot be modified, added or deleted:
The "pseudo-immutability" trap of tuples
The immutability of tuples refers to the immutability of the "reference address" of each element in the tuple**! If the element itself is a mutable object (such as list, dict, set), we can modify the content of this mutable object! This is a pitfall that novices often step into:
Tuple Unpacking - the core operation of Pythonic!
Tuple unpacking can assign the elements of the tuple to multiple variables at one time, which is super practical!
List vs Tuple: Core Comparison Cheat Sheet
A small test of skills (practice questions)
Expected output:
Core points & scene shorthand
- Ordered sequence dual containers: Both list and tuple support common operations such as indexing, slicing, len(), and iteration;
- The core difference lies in “change”:
- List:
[], variable, flexible priority;- Tuple:
()(Add a comma to a single element!), pseudo-immutable, performance/security priority;- Pitfall reminder: The content of variable elements in Tuple can be changed, but the reference cannot be changed;
- Must learn tuple unpacking: This is one of the cores of Python code simplicity.
Mastering lists and tuples is the first step in Python programming. They will be everywhere in the subsequent learning of loops, functions, and dictionaries. Write a few more practice questions and you will be able to master them soon!

