2 min read•june 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.
max()
function finds the maximum value in a list (e.g., max([1, 2, 3, 4])
returns 4
)
min()
function finds the minimum value in a list (e.g., min([1, 2, 3, 4])
returns 1
)
sum()
function calculates the sum of all elements in a list (e.g., sum([1, 2, 3, 4])
returns 10
)
copy()
method creates a new copy of a list (e.g., new_list = old_list.copy()
)
[:]
can also create a shallow copy of a list (e.g., new_list = old_list[:]
)my_list[0]
returns the first element)
my_list[1:4]
returns a new list with elements from index 1 to 3)
[list[start:end:step]](https://www.fiveableKeyTerm:list[start:end:step])
start
: inclusive starting index (default: 0)end
: exclusive ending index (default: length of the list)step
: step value for the slice (default: 1)my_list[0] = 10
changes the first element to 10my_list[1:3] = [20, 30]
replaces elements from index 1 to 2 with [20, 30]
+
operator to combine two or more lists