Knowledge Card - Common List Operations
Common List Operations
-
Appending Elements: Add elements to the end of a list.
- Example:
my_list.append(5)
adds5
tomy_list
.
- Example:
-
Removing Elements: Remove specific elements from a list.
- Example:
my_list.remove('apple')
removes'apple'
frommy_list
.
- Example:
-
Accessing Elements: Retrieve elements by index.
- Example:
my_list[0]
returns the first element.
- Example:
-
Slicing Lists: Extract sublists using slicing.
- Example:
my_list[1:3]
returns elements at indices1
and2
.
- Example:
-
Modifying Elements: Change elements at specific indices.
- Example:
my_list[0] = 'banana'
replaces the first element.
- Example: