Python lists are a fundamental data structure used to store an ordered, mutable collection of items. They are defined using square brackets [] and can hold elements of different data types, including numbers, strings, booleans, or even other lists.
Ordered: Items maintain their sequence, and the order does not change unless explicitly modified.
Mutable: Items can be added, removed, or changed after the list is created.
Indexed: Access elements using zero-based indexing (e.g.,
list[0]for the first item).Allow duplicates: Multiple occurrences of the same value are permitted.
Common Operations and Methods
Create a list:
fruits = ['apple', 'banana', 'cherry'] empty_list = []Add items:
append(): Adds an item to the end.fruits.append('date')insert(): Inserts at a specific index.fruits.insert(1, 'avocado')extend(): Adds multiple items from another list.more_fruits = ['fig', 'grape'] fruits.extend(more_fruits)
Remove items:
remove(): Removes the first occurrence of a value.pop(): Removes and returns an item by index (or last item if no index).clear(): Removes all items.
Access and modify:
Access via index:
fruits[0]→'apple'Modify:
fruits[1] = 'blueberry'
Other useful functions:
len(list)→ returns the number of items.list.sort()→ sorts the list in place.sorted(list)→ returns a new sorted list.list.reverse()→ reverses the list in place.
Use Cases
Lists are ideal for dynamic data such as:
Shopping carts
Task lists
User inputs
Data processing (with libraries like NumPy or Pandas)
Game grids (using nested lists)
For more advanced structures, lists of lists can represent matrices or tables, enabling multi-dimensional data handling.
💡 Tip: Use
list()constructor to convert other iterables (like tuples or strings) into lists:numbers = list((1, 2, 3))
Python list
python - What does list[x::y] do? - Stack Overflow
The Most Complete List of Legally Free Python Books (Updated 2021)
Python script for interfaces into a string / list... skip first line?
Videos
Can someone give me a break down on python lists I'm not understanding
It slices
x[startAt:endBefore:skip]
if you use skip = 2, every other element the list beginning at startAt and ending at endBefore will be selected. [Remember: indices live BETWEEN list elements]
To see this, enter
x = range(100)
at the Python prompt. Then try these things
x[::2]
x[::3]
x[10:40:6]
and see what happens.
L[x::y] means a slice of L where the x is the index to start from and y is the step size. Here are some examples you can try in the interpreter
>>> L=range(20)
>>> L
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
If you want every 3rd element
>>> L[::3]
[0, 3, 6, 9, 12, 15, 18]
Now every third element starting from L[1]
>>> L[1::3]
[1, 4, 7, 10, 13, 16, 19]
Now every third element starting from L[2]
>>> L[2::3]
[2, 5, 8, 11, 14, 17]
You can specify a negative step to go backwards
>>> L[::-1]
[19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
You can also assign to this slice, but the value must have the same length as the slice you are replacing
>>> L[::3]=[0,0,0,0,0,0,0]
>>> L
[0, 1, 2, 0, 4, 5, 0, 7, 8, 0, 10, 11, 0, 13, 14, 0, 16, 17, 0, 19]
Finally, you can delete every 3rd element like this
>>> del L[::3]
>>> L
[1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19]