How to delete specific elements of arrays without listing index
Question:
Attendance = ['Holly','James','Barry','John','Lewy']
Present = input("Is number Holly present?")
if Present == "N":
Attendance.pop("Holly")
print(Attendance[0])
Is there anyway to delete a specific term from the array by name? This doesn’t seem to work? Or can i only delete a specific element of the array by listing its index?
Answers:
Yes you can by using LIST_VARIABLE.remove(‘ELEMENT_TO_BE_REMOVED‘)
Attendance = ['Holly','James','Barry','John','Lewy']
print(Attendance)
Attendance.remove('Holly')
print(Attendance)
The output will be:
['Holly', 'James', 'Barry', 'John', 'Lewy']
['James', 'Barry', 'John', 'Lewy']
Attendance = ['Holly','James','Barry','John','Lewy']
Present = input("Is number Holly present?")
if Present == "N":
Attendance.pop("Holly")
print(Attendance[0])
Is there anyway to delete a specific term from the array by name? This doesn’t seem to work? Or can i only delete a specific element of the array by listing its index?
Yes you can by using LIST_VARIABLE.remove(‘ELEMENT_TO_BE_REMOVED‘)
Attendance = ['Holly','James','Barry','John','Lewy']
print(Attendance)
Attendance.remove('Holly')
print(Attendance)
The output will be:
['Holly', 'James', 'Barry', 'John', 'Lewy']
['James', 'Barry', 'John', 'Lewy']