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

3.4 List basics

3 min readjune 24, 2024

Lists Python are versatile and powerful. They allow you to store and manipulate collections of data easily. You can access, modify, and perform operations on elements using simple syntax and built-in methods.

List manipulation is a key skill in Python programming. Understanding operations like , , and methods like and will help you work efficiently with lists. These tools give you flexibility in managing data structures.

List Fundamentals

List element access and modification

Top images from around the web for List element access and modification
Top images from around the web for List element access and modification
  • Lists are ordered collections of elements enclosed in [] (my_list = [1, 2, 3])
  • Each element in a list is assigned a unique starting from 0 for the first element
  • Access an element using the list name followed by the index in square brackets: list_name[index]
    • my_list[0] returns 1
  • Modify an element by assigning a new value to the specific index: list_name[index] = new_value
    • my_list[1] = 4 changes the second element to 4 resulting in [1, 4, 3]
  • access elements from the end of the list
    • my_list[-1] returns the last element 3
  • Lists are a type of , allowing for over their elements

Determining list length

  • The function determines the number of elements in a list
  • Takes the list as an argument and returns an integer representing the length
  • Syntax: len(list_name)
    • len(my_list) returns 3 if my_list = [1, 2, 3]

Mutability of Python lists

  • Lists in Python are mutable meaning their elements can be changed after the list is created
  • Modify add or remove elements from a list without creating a new object
  • Different from immutable data types like strings or tuples where any changes create a new object
  • Modifying list elements:
    • Change the value of an element using its index: my_list[1] = 4
    • Add elements using methods like append() or : my_list.append(5)
    • Remove elements using methods like remove() or : my_list.remove(2)
  • Be cautious of when working with mutable objects like lists

List Manipulation

Operations for list manipulation

  • Lists support various operations and have built-in methods for manipulation
  • Concatenation: Combine two lists using the + operator
    • [1, 2] + [3, 4] results in [1, 2, 3, 4]
  • : Multiply a list by an integer to repeat its elements
    • [1, 2] * 3 results in [1, 2, 1, 2, 1, 2]
  • Slicing: Extract a portion of a list using the slicing syntax list_name[start:end:step]
    • my_list[1:4] returns a new list with elements from index 1 to 3
  • append(): Add an element to the end of the list
    • my_list.append(5) adds 5 to the end of the list
  • insert(): Insert an element at a specific index
    • my_list.insert(1, 6) inserts 6 at index 1
  • remove(): Remove the first occurrence of a specified element
    • my_list.remove(2) removes the first occurrence of 2 from the list
  • pop(): Remove and return the element at a specified index (default: last element)
    • my_list.pop() removes and returns the last element
  • : Sort the elements of the list in ascending order
    • my_list.sort() sorts the list in place
  • : Reverse the order of the elements in the list
    • my_list.reverse() reverses the order of the elements in place
  • Lists can contain other lists, creating for more complex data structures

Advanced List Concepts

  • : A concise way to create lists based on existing lists or other iterables
  • : Creating a new list that references the same objects as the original list
  • : Creating a new list with new objects that have the same values as the original list
© 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