Skip to main content

How To Change List Elements In Python?

Last Updated: Asked By 1 Answers Answered

Here is a list in Python,

    list = ["Red", "Blue", "Green", "White"]

I want to replace "Green" with "Yellow". How to do this in Python?

python list
Share:

1 Answer

S
Sadique Community Contributor Answered on

To change the specific value in a list, refer to the index of that particular value.

The index of the list will start from 0 to n..

In your case, Red stands for the 0th index, Blue stands for the 1st index, Green stands for the 2nd Index, and So on.

So, you have to replace the second index value.

It is simple. Follow this syntax to replace a particular index in a list

    list_name['index'] = value

Where list_name is the name of the list, index refers to the index position and value refers to the change value.

In your case, list[2] = "Yellow". Hence it is solved. 

Your Answer

Other Resources

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

Q&A Image
Q&A

Ask your coding-related doubts and get answers.

Certification Image
Certification

Earn certifications to enhance your resume.

internships Image
Internships

Hands-on projects to improve your skills.

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

blog Image
Blogs

Add your technical blogs and read technical topics.

Certification Image
Certifications

Earn certifications to enhance your resume.