1

I have a list of file names in my current working directory.

my_list = ["apple.txt","mango.txt", "grapes.txt","draw.png" , "hello123.txt" , "figure.png"]

Now I would like to create a new list which stores only *.txt files:

new_list = ["apple.txt","mango.txt", "grapes.txt", "hello123.txt"]

Is there any way to achieve this using regular expression and pattern matching in Python.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
deepu
  • 147
  • 2
  • 12

3 Answers3

3

You can use this :

new_list = [name for name in my_list if name.endswith('.txt')]
3kt
  • 2,543
  • 1
  • 17
  • 29
3

Method 1 with regex.

import re
txt_regex = re.compile(r'(\w+.txt)')
my_list = ["apple.txt","mango.txt", "grapes.txt","draw.png" , "hello123.txt" , "figure.png"]
result = [i for i in my_list if txt_regex.match(i)]

Demo of regex.

Method 2 with os

from os.path import splitext
result = [i for i in my_list if splitext(i)[1] == '.txt']

Method 3 with split

result = [i for i in my_list if i.split('.')[1] in '.txt']

Ouput

['apple.txt', 'mango.txt', 'grapes.txt', 'hello123.txt']
Rahul K P
  • 15,740
  • 4
  • 35
  • 52
1

You can try this also:

new_list = []
for file in my_list:
    if file.endswith(".txt"):
        new_list.append(file)
print(new_list)

Output

['apple.txt', 'mango.txt', 'grapes.txt', 'hello123.txt']

UPDATE:

You can also group all the files using a defaultdict, like this:

from collections import defaultdict

d = defaultdict(list)
for file in my_list:
    key = "." + file.split(".")[1]
    d[key].append(file)
print(d)

Output:

defaultdict(<class 'list'>, {'.txt': ['apple.txt', 'mango.txt', 'grapes.txt', 'hello123.txt'], '.png': ['draw.png', 'figure.png']})

Or even with out a defaultdict:

d = {}
for file in my_list:
    key = "." + file.split(".")[1]
    if key not in d:
       d[key] = []
    d[key].append(file)
print(d)

Output:

{'.txt': ['apple.txt', 'mango.txt', 'grapes.txt', 'hello123.txt'], '.png': ['draw.png', 'figure.png']}
RoadRunner
  • 25,803
  • 6
  • 42
  • 75