Working with Python Lists and Tuples

A list is an ordered, mutable collection that can hold elements of any type, including other lists.

Creating Lists

Lists can be created through direct definition, for loops, and list comprehensions.

Direct Definition

Use square brackets [].

sample_list = [1, 2, 'hello', [3, 4], (5,), {'data': 100}]

Using a For Loop

num_list = []
for j in range(5):
    num_list.append(j)

List Comprehensions

Use the pattern [expression for variable in iterable [if condition]].

>>> cubed_evens = [k**3 for k in range(10) if k % 2 == 0]
>>> print(cubed_evens)
[0, 8, 64, 216, 512]

Slicing and Indexing

List indexing and slicing work similarly to strings for accessing and extracting data. However, unlike strings, lists allow modification via indexing and slicing.

>>> players = ['Maradona', 'Ronaldo', 'Ronaldinho', 'Neymar']
>>> players[2:3] = ['Kaka', 'Cristiano', 'Zidane', 'Lampard']
>>> players
['Maradona', 'Ronaldo', 'Kaka', 'Cristiano', 'Zidane', 'Lampard', 'Neymar']
>>> players[2:5:2] = ['Pirlo', 'Pepe']
>>> players
['Maradona', 'Ronaldo', 'Pirlo', 'Cristiano', 'Pepe', 'Lampard', 'Neymar']

List Methods

List methods provide functionality for adding, removing, searching, sorting, copying, and reversing elemants.

  • append(), insert(), extend()
  • copy()
  • index(), count()
  • remove(), pop(), clear()
  • sort(), reverse()

Adding Elements

append(item) adds an element to the end of the list.

>>> my_list = []
>>> my_list.append(10)
>>> my_list.append(20)
>>> print(my_list)
[10, 20]

insert(position, item) inserts an element at a specific index.

>>> my_list.insert(1, 15)
>>> print(my_list)
[10, 15, 20]

extend(iterable) adds all elements from an iterable to the end of the list. Strings are added as individual characters.

>>> list_a = [1, 2, 3]
>>> list_b = [4, 5, 6]
>>> list_a.extend(list_b)
>>> print(list_a)
[1, 2, 3, 4, 5, 6]
>>> list_a.extend('abc')
>>> print(list_a)
[1, 2, 3, 4, 5, 6, 'a', 'b', 'c']

Copying

copy() returns a shallow copy of the list.

>>> original = [1, 2, [3, 4]]
>>> duplicate = original.copy()
>>> duplicate is original
False

Searching

index(value, start, stop) returns the index of the first occurrence of the value within the optional slice. Raises ValueError if not found.

>>> data = [5, 10, 15, 10]
>>> data.index(10)
1

count(value) returns the number of times a value appears in the list.

>>> data.count(10)
2
>>> data.count(99)
0

Removing Elements

remove(value) removes the first matching value. Raises ValueError if the value is not present.

>>> items = ['a', 'b', 'c', 'a']
>>> items.remove('a')
>>> print(items)
['b', 'c', 'a']

pop(index) removes and returns the element at the given endex (defaults to the last element).

>>> items.pop()
'a'
>>> items.pop(0)
'b'
>>> print(items)
['c']

clear() removes all elements from the list.

>>> items.clear()
>>> print(items)
[]

Sorting and Reversing

sort(key=None, reverse=False) sorts the list in-place.

>>> nums = [13, 1, 7, 22, 4]
>>> nums.sort()
>>> print(nums)
[1, 4, 7, 13, 22]
>>> nums.sort(reverse=True)
>>> print(nums)
[22, 13, 7, 4, 1]

Using a key function for custom sorting:

>>> def get_first(elem):
...     return elem[0]
>>> pairs = [[3, 30], [1, 10], [2, 20]]
>>> pairs.sort(key=get_first)
>>> print(pairs)
[[1, 10], [2, 20], [3, 30]]

reverse() reverses the elements of the list in-place.

>>> seq = [1, 2, 3]
>>> seq.reverse()
>>> print(seq)
[3, 2, 1]

The del Statement

The del statement can delete list elements or slices.

>>> numbers = [10, 20, 30, 40, 50]
>>> del numbers[1:3]
>>> numbers
[10, 40, 50]
>>> del numbers[0]
>>> numbers
[40, 50]

Tuples

A tuple is an immutable sequence. Its elements cannot be reassigned after creation. However, if a tuple contains mutable objects (like a list), the contents of those objects can be modified.

>>> my_tuple = (1, 2, [3, 4], 5)
>>> my_tuple[2].append(99)  # Modifying the inner list is allowed
>>> my_tuple
(1, 2, [3, 4, 99], 5)
>>> my_tuple[1] = 0  # This will raise an error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

A single-element tuple requires a trailing comma.

>>> single = (1,)
>>> type(single)
<class 'tuple'>

Tuple Methods

Tuples have two primary methods: count() and index(), which function identically to their list counterparts.

count(value) returns the number of occurrences of the value.

>>> t = (1, 2, 2, 3, 2)
>>> t.count(2)
3

index(value, start, stop) returns the index of the first occurrence of the value.

>>> t.index(3)
3

Tags: python list tuple data-structures programming

Posted on Fri, 02 Oct 2026 16:10:08 +0000 by leap500