Python Practice › List Programs › Program 93

Replace a word in a list

EasyList Programs · 93 of 100

Write a Python program to replace “Java” with “Python” in a list of words.

Advertisement
Input
["Hello", "student", "are", "learning", "Java", "Its", "Java", "Language"]
Output
['Hello', 'student', 'are', 'learning', 'Python', 'Its', 'Python', 'Language']

Approach

  1. Map each word, replacing the match.

Solution

Python
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.

More list programs

← All 100 List Programs

Leave a Comment