How to Find Common Words in Two Lists Using Python

If you often work with text data or files, you may encounter the need to find & compare common items such as a list of words from different files. This task can be a pain to tackle manually, particularly as you deal with larger lists.

However, by leveraging the power of Python, you can accomplish this task in a jiffy and with minimal effort. Today, we are going to learn how to find common words in two lists using Python.

Step 1: Prepare Word Lists

The first step involves preparing the two lists that contain the words you want to compare. Let’s say we have two lists of fruits:

Step 2: Use Python’s Set Functionality

Python’s set functionality can be particularly useful for this task. Think of sets as lists with no repeated elements. We can convert our lists into sets and apply the intersection method to find the common elements.

Step 3: Print the Common Words

Finally, you can print the common words. Remember that the output is a set, so you might want to convert it back to a list for easier reading or further manipulation.

Running this code, we get:

['apple', 'cherry', 'dates']

Full Code

In Conclusion

In this tutorial, we have seen how easy it is to find common words in two lists using Python. This simple program can be expanded and modified to handle different kinds of tasks, such as comparing lines in two documents or searching for common tags in a collection of blog posts.

With its uncomplicated syntax and powerful libraries, Python proves to be a versatile tool for many data-processing tasks. You can learn more about sets and other Python data types in the official documentation.