Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
+1 vote
1.4k views
in Computer by (54.6k points)

How can we update and delete list in python.

1 Answer

+1 vote
by (89.9k points)
selected by
 
Best answer

You can update single or multiple elements of lists by giving the slice on the left-hand side of the assignment operator, and you can add to elements in a list with the append() method. Following is a simple example:

# !/user/bin/python 

list = [‘physics’, ‘chemistry’, 1997, 2000]; 

print “Value available at index 2 :” 

print list[2]; 

list [2] = 2001; 

print “New value available at index 2 :” 

print list [2];

When the above code is executed it produces the following result:

Value available at index 2 : 

1997 New value available at index 2 : 

2001

Delete List Elements:

To remove a list element, you can use either the del statement if you know exactly which element(s) you are deleting of the remove() method if you do not know.

Following is a simple example : 

# !/user/bin/python 

list1 = [‘physics’, ‘chemistry’, 1997, 2000]; 

print list1; del list1 [2]; 

print “After deleting value at index 2:” 

print list1;

When the above code is executed, it produces the following result:

[‘physics’, ‘chemistry’, 1997, 2001] 

After deleting value at index 2 : 

[‘physics’, ‘chemistry’, 2000]

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...