Python Lists Explained: Indexing, Slicing, and More


Basic Concepts

A list in Python is an ordered collection of items that can hold multiple values of any data type — like numbers, strings, or even other lists.

  • Lists are mutable, meaning you can change their contents after creation (add, remove, or modify elements).

How do you create a list?
They are written inside square brackets [], with items separated by commas.

How do you access elements in a list?

You access elements in a Python list using indexes inside square brackets [].

  • Indexing starts at 0 for the first element.

  • Use the index number to get the element at that position.

You can also use negative indexes to access elements from the end. 

Negative indices in Python lists let you access elements starting from the end of the list instead of the beginning.

  • -1 is the last element

  • -2 is the second last element

  • -3 is the third last, and so on.

If you try to access an index that doesn’t exist (like fruits[5]), Python will give an IndexError.

How do you slice a list?

You slice a list to get a part (a sublist) of it by specifying a start and end index using the syntax:

list[start:end]

  • start is the index where slicing begins (inclusive).
  • end is the index where slicing stops (exclusive, meaning it doesn’t include the element at this index).

Here, it starts at index 1 (banana) and goes up to, but not including, index 4 (elderberry).

Start or end index in a slice can be omitted to make your code cleaner. 

Omitting the start in list slicing means the slice begins from the first element. 

Omitting the end means the slice goes till the last element. 

Omitting both gives the entire list.

Modifying Lists

Adding elements to a list

You can add elements to a list in Python using:

  • append() – Adds a single element to the end of the list.

  • insert() – Inserts an element at a specific position.

  • extend() – Adds all elements from another iterable (like a list) to the end of the list.

Removing elements from a list

Python provides several ways to remove items from a list. Each method serves a different purpose:

1. remove(value) This method removes the first occurrence of the specified value. If the value is not present in the list, it raises an error.

2. pop(index) This method removes and returns the item at the given index. If no index is provided, it removes the last item in the list.

3. clear() This method removes all items from the list, leaving it empty.

Change or update list elements

To update or change the value of an element in a Python list, you can assign a new value to a specific index. Lists are mutable, which means their contents can be changed after creation. You can also update multiple elements at once using slicing.

Concatenation of lists

You can concatenate (combine) two or more lists in Python using the + operator. This creates a new list that contains all the elements from the original lists in the order they appear.

Concatenation does not change the original lists unless you assign the result to a variable.

Repeating Lists (Multiplying Lists) in Python

In Python, you can repeat a list multiple times by using the * operator. This creates a new list where the original list is repeated the specified number of times.

List Properties and Methods

Length of a list

The len() function can be used to determine how many elements are present in a list. It returns an integer representing the total number of items in the list.

Check if an element exists in a list

To check if an element exists in a list, use the in keyword. It returns True if the element is present, otherwise False.

Common methods of list 

Common list methods in Python help in performing operations like sorting, reversing, finding element positions, and counting occurrences:

  • sort() – arranges elements in ascending order. It sorts the list in place, it does not return a new sorted list-meaning you cannot assign its result to another variable. However, if you want a sorted copy while keeping the original list unchanged, you should use sorted().

  • reverse() – reverses the order of elements in a list in place - meaning it modifies the original list and does not return a new list.

  • index(x) – index() method returns the position of the first occurrence of a specified value in the list. If the value isn't found, it raises an error. To find index of nth occurrence of an element, list comprehension or for loop can be used.

  • count(x) – returns the number of times x appears in the list.

Iteration over a list

Iteration over a list means going through each element one by one using loops. The most common way to iterate over a list is with a for loop.

Note: The break statement statement only exits the nearest for or while loop where it is used.

In Python, enumerate() is a built-in function that makes iteration more convenient by providing both the index and value of elements in a list (or any iterable). enumerate() is preferred over range(len(list)) because it’s more readable!

List comprehensions

List comprehension is a concise way to create lists in Python using a single line of code. It’s more efficient and readable than traditional loops when generating new lists based on existing data.

Basic structure: new_list = [expression for item in iterable if condition]

if condition is optional

Few more examples of list comprehension