0

I got an output list in python as shown below. It there any possible way to add ".txt" behind each items in the list?

['img20210216-160215-80_RASM_RO_3227_88', 'img20210216-160215-35_RASM_LI_226_62', 'img20210216-160213-61_FKTH_LIT_231_97', 'img20210216-160218-22_FKTH_RIT_1235_78', 'img20210216-160217-47_FKTH_LIB_239_75', 'img20201017-105223-50']
crazybin98
  • 29
  • 5

2 Answers2

2
lst = ['img20210216-160215-80_RASM_RO_3227_88', 'img20210216-160215-35_RASM_LI_226_62',
       'img20210216-160213-61_FKTH_LIT_231_97', 'img20210216-160218-22_FKTH_RIT_1235_78',
       'img20210216-160217-47_FKTH_LIB_239_75', 'img20201017-105223-50']

new_list = tuple(f"{i}.txt" for i in lst)
print(new_list)
# ('img20210216-160215-80_RASM_RO_3227_88.txt', 'img20210216-160215-35_RASM_LI_226_62.txt',
#  'img20210216-160213-61_FKTH_LIT_231_97.txt', 'img20210216-160218-22_FKTH_RIT_1235_78.txt',
#  'img20210216-160217-47_FKTH_LIB_239_75.txt', 'img20201017-105223-50.txt')

Use a tuple so that you don't get any error while you do file operation with the list of names.

Dorian Turba
  • 3,260
  • 3
  • 23
  • 67
MrBhuyan
  • 151
  • 2
  • 17
1
my_list = ['img20210216-160215-80_RASM_RO_3227_88', 'img20210216-160215-35_RASM_LI_226_62', 'img20210216-160213-61_FKTH_LIT_231_97', 'img20210216-160218-22_FKTH_RIT_1235_78', 'img20210216-160217-47_FKTH_LIB_239_75', 'img20201017-105223-50']

new_list = [i + '.txt' for i in my_list]

will add '.txt' to the end of each item in my_list.

half of a glazier
  • 1,864
  • 2
  • 15
  • 45