

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do we compare two lists in Python?
The easiest way to do this is use sets. Sets will take the lists and take only unique values. Then you can perform a & operation that acts like intersection to get the common objects from the lists.
example
>>> a = [1, 2, 3, 4, 5] >>> b = [9, 8, 7, 6, 5] >>> set(a) & set(b) {5}
You can also use set.intersection function to perform this operation.
example
>>> a = [1, 2, 3, 4, 5] >>> b = [9, 8, 7, 6, 5] >>> set(a).instersection(set(b)) set([5])
- Related Questions & Answers
- How do we compare the elements of two lists in Python?
- How do we compare two tuples in Python?
- How do we compare two dictionaries in Python?
- How to compare two lists in Python?
- How do we compare two arrays in Java?
- How do we compare Python Dates?
- How do we define lists in Python?
- How to compare two lists for equality in C#?
- How do we compare String in Java
- How do you add two lists in Java?
- How can we compare data in two MySQL tables?
- Dividing two lists in Python
- How do we compare the members of enums in Java?
- How To Do Math With Lists in python ?
- Adding two Python lists elements
Advertisements