Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

modifying 2d lists python

class_name_hobbies = [["Jenny", "Breakdancing"], 
                      ["Alexus", "Photography"], 
                      ["Grace", "Soccer"]]

""" "Jenny" changed their mind and is now more interested in "Meditation".
We will need to modify the list to accommodate the change to our 
class_name_hobbies list. To change a value in a two-dimensional list, 
reassign the value using the specific index.""" 

# The list of Jenny is at index 0. The hobby is at index 1. 
class_name_hobbies[0][1] = "Meditation"
print(class_name_hobbies)

Would output:
[["Jenny", "Meditation"], ["Alexus", "Photography"], ["Grace", "Soccer"]]

Negative indices will work as well:
# The list of Grace is the last entry. The hobby is the last element. 
class_name_hobbies[-1][-1] = "Football"
print(class_name_hobbies)

Would output:
[["Jenny", "Meditation"], ["Alexus", "Photography"], 
 ["Grace", "Football"]]
 
PREVIOUS NEXT
Tagged: #modifying #lists #python
ADD COMMENT
Topic
Name
1+4 =