English, asked by ritikg8671, 10 months ago

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.

Answers

Answered by niveditamishra3011
29

Answer:

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 = [i.replace('.hpp','.h') for i in filenames]

print(newfilenames)  

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

Explanation:

Answered by choyon
2

Answer:

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

y=[]

for x in filenames:

   z=x.replace(".hpp",".h")  

   y.append(z)

print(y)

Explanation:

here at first create an empty list to add the future individual element of the "filenames" list then to iterate in filenames list add a for loop and then replace the .hpp to .h and use append to create a new file which name is y and finally print the value of y.

   

Similar questions