Python Practice › List Programs › Program 93
Replace a word in a list
Write a Python program to replace “Java” with “Python” in a list of words.
Input
["Hello", "student", "are", "learning", "Java", "Its", "Java", "Language"]
Output
['Hello', 'student', 'are', 'learning', 'Python', 'Its', 'Python', 'Language']
Approach
- Map each word, replacing the match.
Solution
words = ["Hello", "student", "are", "learning", "Java", "Its", "Java", "Language"]print(["Python" if w == "Java" else w for w in words])Output
['Hello', 'student', 'are', 'learning', 'Python', 'Its', 'Python', 'Language']
Learn the concept
New to this topic? Read the lesson, then come back.