For approaches to retrieving partial matches in a numeric list, go to:
But if you're looking for how to retrieve partial matches for a list of strings, you'll find the best approaches concisely explained in the answer below.
SO: Python list lookup with partial match shows how to return a bool
, if a list
contains an element that partially matches (e.g. begins
, ends
, or contains
) a certain string. But how can you return the element itself, instead of True
or False
Example:
l = ['ones', 'twos', 'threes']
wanted = 'three'
Here, the approach in the linked question will return True
using:
any(s.startswith(wanted) for s in l)
So how can you return the element 'threes'
instead?