VMware ESXi and vSphere Cluster Management

Python Lists: Creating, Accessing, Slicing, and Modifying Collections

Learn how Python lists store ordered values, use indexes and slices, join collections, and support modifications with assignment, append, insert, remove, pop, and del.

A list is an ordered, mutable Python collection that stores zero or more items in one variable. A list item is one value contained in the list. Lists are similar to array-like collections in other programming languages, but Python lists are built in and flexible: they can grow, shrink, and hold values of different types.

Because list items have an order, each item has a position. Python uses an index, or numeric position, to identify and access an item.

Creating a Python list

List literals use square brackets. Separate items with commas, then assign the list to a variable:

my_numbers = [5, 3, 2, 7]
print(my_numbers)

Output:

[5, 3, 2, 7]

This list contains four numeric items. Lists can also contain strings or values of differing types:

colors = ["red", "green", "blue"]
mixed_values = ["ready", 3, True]

For beginner programs, keeping related values together usually makes a list easier to understand.

Accessing list items with indexes

Python uses zero-based indexing. This means the first item has index 0, the second item has index 1, and so on.

my_numbers = [5, 3, 2, 7]
print(my_numbers[0])

Output:

5

Use square brackets after the list variable to access an item. Parentheses would mean a function call, so my_numbers(0) is not list indexing.

Forward and negative indexes

A negative index counts from the end of a list. The index -1 selects the final item, -2 selects the item before it, and so forth.

my_numbers = [5, 3, 2, 7]
print(my_numbers[-1])

Output:

7
List itemForward indexNegative index
50-4
31-3
22-2
73-1

To read an item, choose an index that exists in the list:

my_numbers = [5, 3, 2, 7]
first_number = my_numbers[0]
last_number = my_numbers[-1]
print(first_number)
print(last_number)

Slicing a list

A slice is a selected range of list items. The basic form is list_name[start:stop]. The start position is included, but the stop position is excluded.

my_numbers = [5, 3, 2, 7]
print(my_numbers[1:3])

The slice starts at index 1, which contains 3, and stops before index 3, which contains 7. Therefore, the result is:

[3, 2]

A missing start position means “begin with the first item.” For example, my_numbers[:2] includes indexes 0 and 1, but not index 2:

my_numbers = [5, 3, 2, 7]
print(my_numbers[:2])

Output:

[5, 3]

Remember that ordinary slicing returns a new list. It does not change the original list.

Concatenating lists

Concatenation means combining collections in sequence. The + operator joins two lists into a new list. It does not add their numeric items together.

morning_tasks = ["email", "meeting"]
afternoon_tasks = ["coding", "review"]
all_tasks = morning_tasks + afternoon_tasks
print(all_tasks)

Output:

["email", "meeting", "coding", "review"]

The items from the left list come first, followed by the items from the right list. Both operands must be lists. For example, to combine a list with one new value, use values + [new_value], or use append().

Modifying lists

Lists are mutable, meaning they can be changed after creation. You can replace items, add items, or remove items.

Replacing an item by index

Assign a new value to an existing index:

my_numbers = [5, 3, 2, 7]
print(my_numbers)
my_numbers[1] = 10
print(my_numbers)

Output:

[5, 3, 2, 7]
[5, 10, 2, 7]

The item at index 1 changed from 3 to 10. The list remained the same length.

Adding an item with append()

Append means adding one item to the end of a list. The append() method changes the existing list:

my_numbers = [5, 3, 2, 7]
my_numbers.append(9)
print(my_numbers)

Output:

[5, 3, 2, 7, 9]

Inserting an item at a position

The insert(index, value) method adds an item at a chosen position. Existing items at that position and after it move to the right:

my_numbers = [5, 3, 2, 7]
my_numbers.insert(1, 8)
print(my_numbers)

Output:

[5, 8, 3, 2, 7]

Removing items

Different removal operations use different criteria:

  • remove(value) removes the first matching item by value.
  • pop(index) removes an item by position and returns the removed value. With no index, pop() removes the final item.
  • del list_name[index] removes an item by position without returning it.
numbers = [5, 3, 2, 7]
numbers.remove(3)
print(numbers)

last_number = numbers.pop()
print(last_number)
print(numbers)

del numbers[0]
print(numbers)

Output:

[5, 2, 7]
7
[5, 2]
[2]

remove(3) searched for the value 3. pop() removed the final item, and del numbers[0] removed the item currently at index 0.

Common list operations

OperationSyntaxEffectExample result
Read an itemvalues[0]Gets the item at index 05
Slicevalues[1:3]Creates a range from index 1 through before index 3[3, 2]
Concatenatefirst_list + second_listCreates one list in sequence[1, 2, 3, 4]
Replacevalues[1] = 10Changes the item at index 1[5, 10, 2, 7]
Appendvalues.append(9)Adds one item at the end[5, 3, 2, 7, 9]
Insertvalues.insert(1, 8)Adds an item at index 1[5, 8, 3, 2, 7]
Remove by valuevalues.remove(3)Removes the first matching value[5, 2, 7]
Remove by positionvalues.pop() or del values[0]Removes by index, or removes the last item when no index is supplied to pop()[5, 3, 2] or [3, 2, 7]

Complete modification example

my_numbers = [5, 3, 2, 7]
print(my_numbers)

my_numbers[1] = 10
my_numbers.append(9)
print(my_numbers)

Output:

[5, 3, 2, 7]
[5, 10, 2, 7, 9]

Troubleshooting list code

Index 1 does not return the first item

Python positions begin at zero, so index 1 is the second item. Use index 0 for the first item and trace the values with an index table.

A slice does not include the stop item

The stop position in start:stop is exclusive. Increase the stop position by one when the item at that index should be included.

An IndexError appears

An IndexError means the requested position does not exist. For a list with length four, valid forward indexes are 0 through 3; valid negative indexes are -1 through -4. Check the list and its length before reading or replacing an item.

Parentheses are used for item access

Use list_name[index], with square brackets. Parentheses attempt to call the list as though it were a function.

List concatenation fails

The + operator requires another list when concatenating. Use values + [new_value] for a new combined list, or call values.append(new_value) to change the existing list.

A slice did not change the original list

Ordinary slicing creates a separate list. Store that result if you need the subset, or use indexed assignment when you intentionally want to change the original list.

Key points

  • A list stores multiple values in one variable and preserves their order.
  • List literals use square brackets, with commas between items.
  • Indexes are zero-based: the first item is at index 0.
  • The negative index -1 selects the final item.
  • A slice uses start:stop; the stop position is excluded and the result is a new list.
  • The + operator concatenates two lists in order.
  • Lists are mutable, so assignment, append(), insert(), remove(), pop(), and del can change them.