DEV Community

Cover image for Python has a built in search function. Here's how to use it
Deepjyoti Barman
Deepjyoti Barman

Posted on

Python has a built in search function. Here's how to use it

I was going through the Python docs and guess what I came across? A function that helps you get closest matches to a list of other strings. Now why would that function be useful, you'd ask. Let me give you a proper example.

The problem

About 1 and a half years ago, I was working on playx. It is a tool that lets you stream music from the commandline directly from YouTube. While working on it, we decided that adding a feature to cache the files would be pretty nice. Now, while implementing that, we came across a problem. We needed to check if a song is already cached.

Now, the issue is, on YouTube, one song is uploaded by various channels which means the titles vary and since we were caching the songs using the title of the video from YouTube, that meant that if we did a direct match of the titles, it would be a very inefficient way to check if the song has been cached already.

For example, if we have the song name Sundown. This song might have been saved as Sundown - Gordon Lightfoot. However, the next time user tries to play the same song which had a different title, something like Sundown - Lyrics only | Gordon Lightfoot. In this case, a simple match would indicate that both songs are different and the tool would end up caching both even though the songs are same.

If you got an idea of the problem from reading the above, I think you would agree with me that it is similar to searching a particular file in a list of files.

This was originally posted on my personal blog and can be read here

Top comments (0)