---
title: "How to Intersect Sets in Python: Best Example"
description: "Intersecting sets in python will result in common values present in those. Here's an example of an intersection method that helps data science engineers."
url: https://srinimf.com/2022/10/16/how-to-intersect-sets-in-python-and-get-common-values/
date: 2022-10-16
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2022/10/pexels-photo-6621191.jpeg?fit=1880%2C1253&ssl=1
categories: ["Python"]
tags: ["Python intersect"]
type: post
lang: en
---

# How to Intersect Sets in Python: Best Example

Here’s an example [to use](https://srinimf.com/2016/01/07/dslist-or-3-4-option-different-view-modes-of-data-sets/) Intersect in Python, and differences between Intersect and Difference methods.

*Python intersection method*

Here are two sets (s_1 and s_2). Intersection method applied on these sets to extract common values of both the sets.

ON THIS PAGE

1. [Python logic to intersect sets](https://srinimf.com/2022/10/16/how-to-intersect-sets-in-python-and-get-common-values/#python-logic-to-intersect-sets)
2. [Python: Intersection Vs. Difference](https://srinimf.com/2022/10/16/how-to-intersect-sets-in-python-and-get-common-values/#python-intersection-vs-difference)

## Python logic to intersect sets

Below exclusive example uses two sets. The s_1 and s_2 are two lists. The s_3 is the resultant set that will create after the intersection operation.

```
s_1 = {"kiwi", "banana", "peanut butter"}
s_2 = {'kiwi', 'spinach', 'banana'}
s_3=  s_1.intersection(s_2)
print(s_1)
print(s_2)
print(s_3)
```

Result of intersection

You can see sets s_1 and s_2 in the display. Then, s_3 set, which is the intersection of s_1 and s_2. Here’s the output.

```
{'kiwi', 'peanut butter', 'banana'} #s_1
{'kiwi', 'spinach', 'banana'}   #s_2
{'kiwi', 'banana'}  #s_3

** Process exited - Return Code: 0 **
Press Enter to exit terminal
```

## Python: Intersection Vs. Difference

The[difference() method](https://www.scaler.com/topics/python-set-difference/) opposite to the intersection() method. Which enables our ERP banking application to find the same values in different lists. It is helpful to find matching values (key) and to create joins and unions in data science.

Related

- [6 DB2 SQL Set Operators](https://srinimf.com/2015/02/02/db2-set-operators/)DateFebruary 2, 2015
- [8 Interview Questions on Python, SQL, PySpark, and Databricks with Resolutions](https://srinimf.com/2024/03/09/python-sql-pyspark-and-databricks-8-interview-questions/)DateMarch 9, 2024
- [Efficient Python Dictionary Merge: update(), ** Operator, dict() Constructor](https://srinimf.com/2023/09/29/how-to-merge-python-dictionaries-3-methods/)DateSeptember 29, 2023

-

Recommended

- Learn the most in-demand Artificial Intelligence skills at $39.99 and save $260

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2022%2F10%2F16%2Fhow-to-intersect-sets-in-python-and-get-common-values%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2022%2F10%2Fpexels-photo-6621191.jpeg%3Ffit%3D1200%252C800%26ssl%3D1&description=How%20to%20Intersect%20Sets%20in%20Python%3A%20Best%20Example)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2022/10/16/how-to-intersect-sets-in-python-and-get-common-values/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2022/10/16/how-to-intersect-sets-in-python-and-get-common-values/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2022/10/16/how-to-intersect-sets-in-python-and-get-common-values/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2022/10/16/how-to-intersect-sets-in-python-and-get-common-values/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20How%20to%20Intersect%20Sets%20in%20Python%3A%20Best%20Example&body=https%3A%2F%2Fsrinimf.com%2F2022%2F10%2F16%2Fhow-to-intersect-sets-in-python-and-get-common-values%2F&share=email)
-

### Like this:

Like Loading…