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?

Asked By: Shaneb7102

||

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']
Answered By: Junaid Nazir
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.