[]
.Example:
0
).Example:
list[start:stop:step]
Example:
Example:
append()
to add an element to the end of the list.insert()
to add an element at a specific position.extend()
to add multiple elements from another list.Examples:
remove()
to remove the first occurrence of a value.pop()
to remove an element at a specific index (or the last element if no index is provided).del
to delete an element or a slice of elements.clear()
to remove all elements from the list.Examples:
index()
to find the index of the first occurrence of a value.in
to check if an element exists in the list.Examples:
sort()
to sort the list in place (modifies the original list).sorted()
to return a new sorted list (does not modify the original list).Examples:
reverse()
to reverse the list in place.Example:
len()
to find the number of elements in a list.Example:
[expression for item in iterable if condition]
Examples:
Example:
Creating a List:
Adding Elements:
Removing Elements:
Sorting:
List Comprehension:
append()
instead of +
for adding elements to a list (more efficient).