List Methods

In Python, lists are ordered collections that can store multiple items, which can be of different types. Python provides several built-in methods to manipulate and interact with lists. In this lesson, you'll learn about some common list methods that you can use to modify, format, and analyze lists.

Creating Lists

You can create lists using square brackets [], with each item separated by commas.

numbers = [1, 2, 3, 4, 5]
fruits = ['apple', 'banana', 'cherry']

Accessing List Elements

You can access individual elements in a list using indexing (0-based index). The index starts from 0 for the first element and -1 for the last element.

Example:

fruits = ['apple', 'banana', 'cherry']
print(fruits[0])  # Output: apple
print(fruits[-1])  # Output: cherry (last element)

Explanation:

  • fruits[0] accesses the first element of the list.
  • fruits[-1] accesses the last element of the list.

Slicing Lists

You can extract a part of a list using slicing. Slicing uses the format list[start:end:step].

Example:

numbers = [1, 2, 3, 4, 5]
sub_list = numbers[1:4]  # Extract elements from index 1 to 3
print(sub_list)  # Output: [2, 3, 4]

Explanation:

  • numbers[1:4] extracts elements from index 1 to 3 (not including index 4).

Common List Methods

1. len()

The len() function returns the number of elements in the list.

numbers = [1, 2, 3, 4, 5]
print(len(numbers))  # Output: 5

2. append()

The append() method adds a single element to the end of the list.

fruits = ['apple', 'banana']
fruits.append('cherry')
print(fruits)  # Output: ['apple', 'banana', 'cherry']

3. extend()

The extend() method adds multiple elements (from another list or iterable) to the end of the list.

fruits = ['apple', 'banana']
fruits.extend(['cherry', 'date'])
print(fruits)  # Output: ['apple', 'banana', 'cherry', 'date']

4. insert()

The insert() method adds an element at a specific index in the list.

fruits = ['apple', 'banana']
fruits.insert(1, 'cherry')  # Insert at index 1
print(fruits)  # Output: ['apple', 'cherry', 'banana']

5. remove()

The remove() method removes the first occurrence of the specified element from the list.

fruits = ['apple', 'banana', 'cherry']
fruits.remove('banana')
print(fruits)  # Output: ['apple', 'cherry']

6. pop()

The pop() method removes and returns the element at the specified index (default is the last element).

fruits = ['apple', 'banana', 'cherry']
removed = fruits.pop(1)  # Remove and return the element at index 1
print(removed)  # Output: banana
print(fruits)   # Output: ['apple', 'cherry']

7. index()

The index() method returns the index of the first occurrence of a specified element.

fruits = ['apple', 'banana', 'cherry']
index = fruits.index('banana')
print(index)  # Output: 1

8. count()

The count() method returns the number of occurrences of a specified element in the list.

fruits = ['apple', 'banana', 'cherry', 'banana']
count = fruits.count('banana')
print(count)  # Output: 2

9. sort()

The sort() method sorts the elements of the list in ascending order (modifies the original list).

numbers = [5, 2, 9, 1, 5, 6]
numbers.sort()
print(numbers)  # Output: [1, 2, 5, 5, 6, 9]

10. reverse()

The reverse() method reverses the elements of the list in place.

fruits = ['apple', 'banana', 'cherry']
fruits.reverse()
print(fruits)  # Output: ['cherry', 'banana', 'apple']

11. clear()

The clear() method removes all elements from the list.

fruits = ['apple', 'banana', 'cherry']
fruits.clear()
print(fruits)  # Output: []

List Comprehension

List comprehension provides a concise way to create lists.

Example:

numbers = [1, 2, 3, 4, 5]
squares = [x**2 for x in numbers]
print(squares)  # Output: [1, 4, 9, 16, 25]
even_squares = [x**2 for x in range(10) if x % 2 == 0]
print(even_squares) # Output: [0, 4, 16, 36, 64]

List Formatting Methods

1. join()

The join() method joins a list of strings into a single string, using the specified separator.

words = ['Hello', 'Python', 'World']
sentence = " ".join(words)
print(sentence)  # Output: Hello Python World
Task

Practice: List Manipulation

Objective: Practice using list methods to manipulate and format lists.

  1. Create a Python File: Create a file named list_methods_practice.py.

  2. Add Elements to a List: Use the append() method to add a new element to a list.

    Example:

    fruits = ['apple', 'banana']
    fruits.append('cherry')
    print(fruits)
    
  3. Remove an Element: Use the remove() method to remove an element from a list.

    Example:

    fruits = ['apple', 'banana', 'cherry']
    fruits.remove('banana')
    print(fruits)
    
  4. Sort the List: Use the sort() method to sort a list of numbers in ascending order.

    Example:

    numbers = [5, 2, 9, 1, 5, 6]
    numbers.sort()
    print(numbers)
    
  5. List Comprehension: Use list comprehension to create a list of squares of the numbers from 1 to 5.

    Example:

    numbers = [1, 2, 3, 4, 5]
    squares = [x**2 for x in numbers]
    print(squares)
    

Copyright © 2025 Devship. All rights reserved.

Made by imParth