Master Python Lists and Tuples: Definitions, Operations, and Common Pitfalls
An in‑depth Python tutorial explains the definition, manipulation, and common pitfalls of lists and tuples, covering basic operations, indexing, adding/removing elements, nested structures, immutability nuances, and practical examples illustrated with clear diagrams.
Python list
Python has a built-in ordered collection type called list.
1. List basic definition
A list is an ordered collection that can be modified by adding or removing elements.
Example: a list of classmates' names.
The variable classmates is a list. Use len() to get its length.
Access elements by index starting at 0. Use negative indices to count from the end.
When an index is out of range, Python raises IndexError. The last element index is len(classmates) - 1.
Use -1 to get the last element directly.
Similarly you can retrieve the second‑last, third‑last, etc.
2. Adding and removing elements
Lists are mutable; you can append elements to the end.
Insert an element at a specific position, e.g., index 1.
Remove the last element with pop().
Remove an element at a given index with pop(i).
Replace an element by assigning to its index.
3. Exploring list elements
List elements can be of different data types, and a list can contain another list.
An empty list has length 0.
Python tuple
1. Tuple basic definition
A tuple is an ordered collection similar to a list but immutable.
Tuples lack methods like append() or insert(). Access elements the same way, but you cannot reassign them.
Immutable tuples make code safer; use them when possible.
2. Tuple pitfalls
When defining a tuple with a single element, you must include a trailing comma to avoid it being interpreted as a plain value.
Defining () creates an empty tuple.
Without the comma, (1) is just the number 1.
Python therefore displays a single‑element tuple with a trailing comma.
3. Mutable tuple example
A tuple can contain mutable objects like a list; modifying the list changes the tuple's contents, though the tuple's references remain unchanged.
After changing list elements, the tuple appears changed, but only the list inside was mutated.
Thus, a tuple's immutability refers to the references, not the mutability of contained objects.
Summary
The article compares Python's two ordered collections: mutable lists and immutable tuples, and shows how to use and manipulate them.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
