Skip to Content
Course content

In Python, a list is one of the most commonly used data structures. It is an ordered, mutable collection that can hold a variety of data types. Lists allow you to store multiple items in a single variable, making them a very versatile and important part of Python programming.

Key Features of Lists:

  • Ordered: The elements in a list are stored in a specific order. This means that items have a defined index, starting from 0.
  • Mutable: You can modify a list after it is created. You can add, remove, or change the elements of a list.
  • Can store multiple data types: A list can contain items of different types, such as integers, strings, floats, and even other lists.

Creating a List:

A list is created by enclosing items in square brackets [], with each item separated by commas.

Syntax:

list_name = [item1, item2, item3, ...]

Example:

fruits = ['apple', 'banana', 'cherry', 'date']
numbers = [1, 2, 3, 4, 5]
mixed_list = [1, 'hello', 3.14, True]
  • Explanation:
    • fruits is a list of strings.
    • numbers is a list of integers.
    • mixed_list is a list containing different data types: an integer, a string, a float, and a boolean.

Accessing List Elements:

You can access individual items in a list by referring to their index (position) within the list. Python uses zero-based indexing, meaning the first item is at index 0, the second item at index 1, and so on.

Syntax:

list_name[index]

Example:

fruits = ['apple', 'banana', 'cherry', 'date']
print(fruits[0])  # Output: apple
print(fruits[2])  # Output: cherry
  • Negative Indexing:
    • Python also supports negative indexing, which allows you to access the list from the end.
    • The last item is -1, the second last item is -2, and so on.

Example:

print(fruits[-1])  # Output: date (last item)
print(fruits[-2])  # Output: cherry (second last item)

List Slicing:

You can retrieve a subset of a list using slicing. Slicing returns a portion of the list, specified by a start index and an end index.

Syntax:

list_name[start:end]
  • start: The index where the slice starts (inclusive).
  • end: The index where the slice ends (exclusive).

Example:

fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']
print(fruits[1:4])  # Output: ['banana', 'cherry', 'date']
  • Explanation:
    • The slice starts at index 1 (inclusive) and ends at index 4 (exclusive), so the result is the sublist from banana to date.
  • Omitting start or end:
    • If you omit the start index, it defaults to the beginning of the list.
    • If you omit the end index, it defaults to the end of the list.

Example:

print(fruits[:3])  # Output: ['apple', 'banana', 'cherry']  (from the start to index 3)
print(fruits[2:])  # Output: ['cherry', 'date', 'elderberry']  (from index 2 to the end)

Modifying Lists:

Since lists are mutable, you can change the elements of a list after it has been created.

  • Changing an element: You can change the value of an element at a specific index.

    Example:

    fruits = ['apple', 'banana', 'cherry']
    fruits[1] = 'blueberry'
    print(fruits)  # Output: ['apple', 'blueberry', 'cherry']
    
  • Adding elements: There are several ways to add elements to a list:
    • append(): Adds an item to the end of the list.
    • insert(): Adds an item at a specific index.
    • extend(): Adds multiple items to the end of the list.

    Example:

    fruits.append('date')  # Adds 'date' at the end
    fruits.insert(1, 'elderberry')  # Adds 'elderberry' at index 1
    fruits.extend(['fig', 'grape'])  # Adds multiple items at the end
    print(fruits)  # Output: ['apple', 'elderberry', 'blueberry', 'cherry', 'date', 'fig', 'grape']
    
  • Removing elements: There are several ways to remove elements:
    • remove(): Removes the first occurrence of a value.
    • pop(): Removes and returns an item at a specific index (defaults to the last item).
    • clear(): Removes all items from the list.

    Example:

    fruits.remove('blueberry')  # Removes 'blueberry'
    popped_item = fruits.pop(2)  # Removes and returns the item at index 2
    print(fruits)  # Output: ['apple', 'elderberry', 'cherry', 'date', 'fig', 'grape']
    print(popped_item)  # Output: 'cherry'
    

List Methods:

Here are some useful built-in methods for working with lists:

  • len(): Returns the length (number of elements) of the list.
    len(fruits)  # Output: 6
    
  • sort(): Sorts the list in ascending order.
    fruits.sort()  # Output: ['apple', 'date', 'elderberry', 'fig', 'grape']
    
  • reverse(): Reverses the order of the list.
    fruits.reverse()  # Output: ['grape', 'fig', 'elderberry', 'date', 'apple']
    
  • count(): Returns the number of occurrences of an item in the list.
    fruits.count('apple')  # Output: 1
    
  • index(): Returns the index of the first occurrence of an item.
    fruits.index('date')  # Output: 2
    

Summary:

  • A list in Python is an ordered and mutable collection.
  • You can access elements using indices (both positive and negative).
  • Lists can store multiple types of data, and you can modify them with various methods like append(), insert(), remove(), etc.
  • Slicing allows you to extract parts of a list.
  • Lists are essential in Python for managing and manipulating collections of data.

Lists are one of the most flexible and powerful data structures in Python, and understanding them is key to mastering the language.

Commenting is not enabled on this course.