'find files with similar ID from different folder

I have two folder A and B. in each folder I have text files containing participant IDs starting from 100. some files can be present in both folders. i want to check if both the file names match each other by id and get them.

file name example: result_100.txt

I have written a logic, but it is taking too long to find.

def find_id:
    returns id

for i in folder_A:
    for j in forder_b:
        if find_id(i) == find_id(j):
            print(i, "--", j)


Solution 1:[1]

Easy way.

import os
first = os.listdir('Enter Your Path To Folder A')
second = os.listdir('Enter Your Path To Folder B')
same = list(set(first).intersection(set(second)))

print(same)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sharim Iqbal