SantoshGupta7
SantoshGupta7

Reputation: 6197

Is it possible to check a set for a partial string in Python?

Say that I have a set of strings. Is it possible to search using a partial string, and if any of the values in the set contain that partial string, return the full string?

example:

thisSet = { 'orange', 'orabolo', 'apple', 'dog'}
partialString = 'ora'

setFunction(thisSet, partialString) 

> 'orange', 'orabolo'

Upvotes: 1

Views: 1660

Answers (1)

A.J. Uppal
A.J. Uppal

Reputation: 19264

Use a simple set comprehension:

{i for i in thisSet if partialString in i}

>>> thisSet = {'orange', 'orabolo', 'apple', 'dog'}
>>> partialString = 'ora'
>>> {i for i in thisSet if partialString in i}
{'orange', 'orabolo'}
>>> 

Upvotes: 3

Related Questions