You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

9.3 Common list operations

2 min readjune 24, 2024

Lists in Python are versatile and powerful. They offer various built-in functions and methods for manipulation, like , , and sum(). Copying lists with copy() or slice notation creates separate objects, allowing safe modifications.

List slicing extracts portions of lists using index ranges. It's a flexible way to access, modify, or create new lists. Advanced operations like and expand the possibilities for working with complex data structures.

List Operations and Manipulation

Built-in functions for lists

Top images from around the web for Built-in functions for lists
Top images from around the web for Built-in functions for lists
  • max()
    function finds the maximum value in a list (e.g.,
    max([1, 2, 3, 4])
    returns
    4
    )
    • Works with numeric values and strings (returns maximum based on alphabetical order for strings)
  • min()
    function finds the minimum value in a list (e.g.,
    min([1, 2, 3, 4])
    returns
    1
    )
    • Works with numeric values and strings (returns minimum based on alphabetical order for strings)
  • sum()
    function calculates the sum of all elements in a list (e.g.,
    sum([1, 2, 3, 4])
    returns
    10
    )
    • Works with lists containing numeric values

Copying lists with copy()

  • copy()
    method creates a new copy of a list (e.g.,
    new_list = old_list.copy()
    )
    • The new list is a separate object in memory, so modifying it does not affect the original list
    • Useful when working with a list without modifying the original data
  • Slice notation
    [:]
    can also create a shallow copy of a list (e.g.,
    new_list = old_list[:]
    )

List manipulation through slicing

  • Indexing accesses individual elements of a list using their index (e.g.,
    my_list[0]
    returns the first element)
    • Index starts at 0 for the first element and supports negative indexing (-1 for the last element)
  • Slicing extracts a portion of a list using a range of indices (e.g.,
    my_list[1:4]
    returns a new list with elements from index 1 to 3)
    • Syntax:
      [list[start:end:step]](https://www.fiveableKeyTerm:list[start:end:step])
      1. start
        : inclusive starting index (default: 0)
      2. end
        : exclusive ending index (default: length of the list)
      3. step
        : step value for the slice (default: 1)
  • Modifying elements by assigning new values to specific indices or slices
    • Example:
      my_list[0] = 10
      changes the first element to 10
    • Example:
      my_list[1:3] = [20, 30]
      replaces elements from index 1 to 2 with
      [20, 30]
  • can be performed using the
    +
    operator to combine two or more lists

Advanced List Operations

  • provides a concise way to create lists based on existing lists or other iterable objects
  • Nested lists allow for the creation of multi-dimensional data structures, where elements of a list can themselves be lists
  • (such as append(), extend(), insert(), remove(), and sort()) provide additional ways to manipulate lists
  • Lists are mutable, meaning their contents can be changed after creation, unlike immutable data types
  • through lists can be done using loops, allowing for efficient processing of list elements
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary