0

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.
new_filename=[]
new_list=[]
final_file=[]
for element in filenames:
    if element.endswith("p"):
        new_filename.append(element)
for element1 in new_filename:
    new_list.append(element1.split("pp")[0])
for element3 in filenames:
    if not element3.endswith("p"):
        final_file.append(element3)
final_file.extend(new_list)
print(final_file)
# Should be ["program.c", "stdio.h", "sample.h", "a.out", "math.h", "hpp.out"]

Is there any way to extend new_list to final_file at the index[1]?

Is there any simpler solutions?

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    there could be a one-line solution to this but I would not recommend it because seems like this is an assignment made for the incremental learning of loops and other python things. – Vishal Singh Sep 10 '20 at 07:53
  • You can easily solve this in one for-loop using an if-else statement. You've already started in the right direction in your first loop. Just extend the first if-statement to: `if element.endswith(".hpp"):`, then you will match all of the files that you want to match, and you can append a sliced version of that string into a new list. Then you can append all others into the same list in an else-statement. – Hampus Larsson Sep 10 '20 at 07:55
  • thanks for the suggestions i am hoping to find an easier method – NELSON JOSEPH Sep 10 '20 at 17:10

1 Answers1

0

a simpler way to do it:

filenames = ["program.c", "stdio.hpp", "sample.hpp", "a.out", "math.hpp", "hpp.out"]
final_file=[]
for file in filenames:
    if file.endswith(".hpp"):
        final_file.append(file.replace('.hpp','.h'))
    else:
        final_file.append(file)

replace() will just replace a sub-string with another

Azeer Esmail
  • 124
  • 2
  • 3
  • you're right, i missed that file's name can contain hpp, i just edited the answer, thanks!! – Azeer Esmail Sep 10 '20 at 11:20
  • You updated code will still replace `hpp` if it is found anywhere in the filename. The only difference now is that it also has to end with `hpp`. The main purpose of OP's question is to only change files that has the **extension** `hpp`. If any of the following files are in the list, then they shouldn't be changed: `["file.hhpp", "ffshhp"]`, while the following should only change the **extension**: `[".hhp.hhp", "fishhp.hhp"]` – Hampus Larsson Sep 10 '20 at 12:43
  • i added the dot before, it should be good now, unless the name contains .hpp which i dont think it's likely – Azeer Esmail Sep 10 '20 at 12:56
  • thanks alot thats a great help for me i am a begineer thanks for the suggestions – NELSON JOSEPH Sep 10 '20 at 17:08