Explore Topics

How To Change Range Of Values Item Values In List?

Last Updated : 18 Apr, 2025 - Asked By Ashok

Here is a list in Python,

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

I want to replace "Blue" and "Green" with "gray" and "silver." How can I do this in Python?

python  range of values 

Answers
2024-10-02 11:59:38

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

       list = ["Red", "Blue", "Green", "White","Yellow"]
       list[1:3] = ["gray", "silver"]
       print(list)

Suppose if you insert more items, your output will be like this,

       list = ["Red", "Blue", "Green", "White","Yellow"]
       list[1:3] = ["gray", "silver","silver","silver","silver","silver","silver"]
       print(list)

OUTPUT: ['Red', 'gray', 'silver', 'silver', 'silver', 'silver', 'silver', 'silver', 'White', 'Yellow'

Suppose if you insert only one list element into the range of Values; your output will be like this

    list = ["Red", "Blue", "Green"]
    list[1:3] = ["gray"]
    print(list)

    O/P: ['Red', 'gray']

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.