-2

I have a list which contains multiple filename:

example_list = ['2021-08-09_01.csv', '2021-08-09_02.csv', '2021-08-10_12.csv',
                '2021-08-10_03.csv']

I want to return a new list which includes the days that are inside the example_list:

new_list = ['09','10']

Regardless the number after the day string, what's the easiest way to achieve this?

martineau
  • 119,623
  • 25
  • 170
  • 301
wawawa
  • 2,835
  • 6
  • 44
  • 105
  • create new empy list, add all the files name splitted (use split() twice or see here https://stackoverflow.com/questions/56718450/how-can-i-split-between-two-characters-in-a-list-element-in-python for re.split) to get the day and then use set() to get each day just once – pippo1980 Aug 10 '21 at 17:21

3 Answers3

4

You can use a set comprehension where you slice the needed part from the string elements, and then (if actually needed) convert the created set into a list:

example_list = ['2021-08-09_01.csv', '2021-08-09_02.csv', '2021-08-10_12.csv', '2021-08-10_03.csv']

new_list = list({elem[8:10] for elem in example_list})

print(new_list)

Output:

['09', '10']
ruohola
  • 21,987
  • 6
  • 62
  • 97
0

You can use str.split to get the day string and then use set() to remove duplicates:

new_list = list(set(s.split("-")[-1].split("_")[0] for s in example_list))
print(new_list)

Prints:

['10', '09']
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
0

You could try like this.

Split the sub-string (i[8:]) at _ and select the first item. Use a set to remove duplicates.

example_list = ['2021-08-09_01.csv', '2021-08-09_02.csv', '2021-08-10_12.csv', '2021-08-10_03.csv']

s = {i[8:].split('_')[0] for i in example_list}
print(list(s))
['09', '10']
Ram
  • 4,724
  • 2
  • 14
  • 22