'how to modify element of list in python

Given a list of filenames, we want to rename all the files with extension .hpp to the extension h. To do this, we would like to generate a new list called newfilenames, consisting of the new filenames. Fill in the blanks in the code using any of the methods you’ve learned thus far, like a for loop or a list comprehension.

filenames = ["program.c", "stdio.hpp", "sample.hpp", "a.out", "math.hpp", "hpp.out"]
# Generate newfilenames as a list containing the new filenames
# using as many lines of code as your chosen method requires.

print(newfilenames) 
# Should be ["program.c", "stdio.h", "sample.h", "a.out", "math.h", "hpp.out"]


Solution 1:[1]

newfilenames = [e.replace('.hpp','.h') for e in filenames]

Solution 2:[2]

filenames = ["program.c", "stdio.hpp", "sample.hpp", "a.out", "math.hpp", "hpp.out"]

newfilenames=[]

for filename in filenames:
    if filename.endswith(".hpp"):
        filename = filename.replace(".hpp", ".h")
        newfilenames.append(filename)
        
    else:
        newfilenames.append(filename)


print(newfilenames)

Solution 3:[3]

filenames = ["program.c", "stdio.hpp", "sample.hpp", "a.out", "math.hpp", "hpp.out"]
# Generate newfilenames as a list containing the new filenames
# using as many lines of code as your chosen method requires.
newfilenames = [word.replace("hpp","h") if word.endswith("hpp") else word for word in filenames ]

print(newfilenames) 
# Should be ["program.c", "stdio.h", "sample.h", "a.out", "math.h", "hpp.out"]

Solution 4:[4]

filenames = ["program.c", "stdio.hpp", "sample.hpp", "a.out", "math.hpp", "hpp.out"]

newfilenames = []
for names in filenames:
    if names.endswith('.hpp'):
        newfilenames.append(names[:-2])
        continue
    newfilenames.append(names)


print(newfilenames) 

Solution 5:[5]

filenames = ["program.c", "stdio.hpp", "sample.hpp", "a.out", "math.hpp", "hpp.out"]

newfilenames=[]
for x in filenames:
    if x.endswith(".hpp"):
        newfilenames.append(x.replace(".hpp",".h"))
    else:
        newfilenames.append(x)     

print(newfilenames) 

Solution 6:[6]

filenames = ["program.c", "stdio.hpp", "sample.hpp", "a.out", "math.hpp", "hpp.out"]
newfilenames=[]
for filename in filenames:
    if '.hpp' in filename:
        index=filename.index(".")
        newfile=filename[:index]+".h"
        newfilenames.append(newfile)
    else:
        newfilenames.append(filename)

print(filenames)

Solution 7:[7]

newfilenames = [ f.rstrip("pp") if f.endswith(".hpp") else f for f in filenames ]

Instead of str.replace(), I prefer using str.rstrip() and str.endswith() to take care of potential edge cases.

Solution 8:[8]

newfilenames = []
for names in filenames:
    if names.endswith('.hpp'):
        newfilenames.append(names[:-2])
        continue
    newfilenames.append(names)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bp7070
Solution 2 ree_d_wan
Solution 3
Solution 4 Gryu
Solution 5 Roshin Raphel
Solution 6 RAKESH AGARWAL
Solution 7 zeal4learning
Solution 8 Roberto Caboni