

- 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
Python - Total equal pairs in List
When it is required to find the total equal pairs in a list, the ‘set’ operator and the ‘//’ operator along with an iteration can be used.
Example
Below is a demonstration of the same
my_list = [34, 56, 12, 32, 78, 99, 67, 34, 52, 78, 99, 10, 0, 11, 23,9] print("The list is :") print(my_list) all_elems = set(my_list) my_result = 0 for elements in all_elems: my_result += my_list.count(elements) // 2 print("The total pairs are :") print(my_result)
Output
The list is : [34, 56, 12, 32, 78, 99, 67, 34, 52, 78, 99, 10, 0, 11, 23, 9] The total pairs are : 3
Explanation
A list is defined and is displayed on the console.
The list is converted into a set and assigned to a variable.
An integer is assigned the value of 0.
The elements in the set are iterated over, and the ‘//’ operator is used.
This is added to the result.
This is the output that is displayed on the console.
- Related Questions & Answers
- Pairs of Songs With Total Durations Divisible by 60 in Python
- Python – Cross Pattern Pairs in List
- Subarray pairs with equal sums in JavaScript
- Find all distinct pairs with difference equal to k in Python
- Python - Split list into all possible tuple pairs
- Program to find out the number of pairs of equal substrings in Python
- Program to find minimum total cost for equalizing list elements in Python
- Making List Values Equal in C++
- Count all distinct pairs with difference equal to k in C++
- Program to find total unique duration from a list of intervals in Python
- Print all pairs in an unsorted array with equal sum in C++
- Count of index pairs with equal elements in an array in C++
- Find pairs with given product in a sorted Doubly Linked List in Python
- Count pairs from two arrays having sum equal to K in C++
- How to count total number of occurrences of an object in a Python list?
Advertisements