Python Lists: From Basics to Nested Lists
2
min read
.
Updated on
02 Sep 2025
Python lists are mutable ordered collections and one of the most commonly used data structures in the language. They work well for sequences that need to grow, shrink, reorder, or contain arbitrary Python objects.
Create Lists
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
empty_list = []
numbers = [1 , 2 , 3 , 4 , 5 ]
mixed = [1 , "Hello" , 3.14 , True ]
A list can contain values of different types, although homogeneous lists are often easier to process predictably.
Access and Slice Items
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
numbers = [10 , 20 , 30 , 40 , 50 ]
print(numbers[0 ])
print(numbers[- 1 ])
print(numbers[1 :4 ])
Slicing returns a new list.
Add Items
Append one item:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
Add all items from another iterable:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
Insert at a specific position:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
Repeated insertion near the front of a large list can be expensive because later elements need to be shifted.
Remove Items
Remove the first matching value:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
Remove and return an item by index:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
Delete without returning the value:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
Clear all items:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
Update Items
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
numbers = [1 , 2 , 3 ]
numbers[1 ] = 20
Slice assignment can replace multiple items:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
numbers[1 :3 ] = [20 , 30 , 40 ]
Loop Through a List
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
for number in numbers:
print(number)
When you need the index as well:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
for index, number in enumerate(numbers):
print(index, number)
List Comprehensions
Build transformed or filtered lists concisely:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
squares = [number * number for number in range(6 )]
even_squares = [number * number for number in range(10 ) if number % 2 == 0 ]
Prefer an ordinary loop when the transformation has multiple steps or side effects.
Sorting
Sort a list in place:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
Create a sorted copy:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
sorted_numbers = sorted(numbers)
Sort complex objects with a key function:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
users = [
{"name" : "Alice" , "age" : 30 },
{"name" : "Bob" , "age" : 25 },
]
users. sort(key= lambda user: user["age" ])
Nested Lists
Lists can model grids or nested data:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
matrix = [
[1 , 2 , 3 ],
[4 , 5 , 6 ],
]
print(matrix[1 ][2 ]) # 6
Be careful when creating repeated nested lists. This creates shared inner objects:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
Prefer:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
rows = [[0 ] * 3 for _ in range(2 )]
so each row is a separate list.
Copying Lists
A shallow copy can be created with:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
For nested mutable objects, use copy.deepcopy() only when you genuinely need an independent recursive copy.
Conclusion
Lists are Python’s general-purpose mutable sequence. Learn indexing, slicing, mutation methods, comprehensions, sorting, and the behavior of nested mutable objects to use them safely and efficiently.