Just_Newbie
Just_Newbie

Reputation: 447

How to find common strings in elements of the same list (not using intersection)

I am having a list like

a=['foo', 'boo', 'foo_ext', 'doo']

I want to find if I have elements that differ by a given string, for example, _ext in my case - so the output should return that I have 'foo' and 'foo_ext'

I ve tried with intersection, but that returns exact matches, not "partial" ones like I need..

Upvotes: 0

Views: 44

Answers (1)

lauriane.g
lauriane.g

Reputation: 337

You can find those elements like that

for i in range(len(a)):
    for j in range(i+1,len(a)):
        if a[i] in a[j] or a[j] in a[i]:
            print(a[i],a[j])

The output is foo foo_ext.

Edit : I am not sure this answer your problem. Do you want only elements which differ by the particular string _ext?

Upvotes: 1

Related Questions