Common operations for lists in Python include:
-
Creating a List:
my_list = [1, 2, 3] -
Accessing Elements:
first_element = my_list[0] # Accesses the first element -
Appending Elements:
my_list.append(4) # Adds 4 to the end of the list -
Inserting Elements:
my_list.insert(1, 1.5) # Inserts 1.5 at index 1 -
Removing Elements:
my_list.remove(2) # Removes the first occurrence of 2 -
Popping Elements:
last_element = my_list.pop() # Removes and returns the last element -
Slicing:
sub_list = my_list[1:3] # Gets elements from index 1 to 2 -
Sorting:
my_list.sort() # Sorts the list in place -
Reversing:
my_list.reverse() # Reverses the list in place
These operations allow you to effectively manage and manipulate list data in Python. If you need examples or further details on any specific operation, feel free to ask!
