
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 Articles
- Python – Cross Pattern Pairs in List
- Pairs of Songs With Total Durations Divisible by 60 in Python
- 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
- Subarray pairs with equal sums in JavaScript
- Python Program to Alternate list elements as key-value pairs
- Find pairs with given product in a sorted Doubly Linked List in Python
- Program to find minimum total cost for equalizing list elements in Python
- Python program to find sum of absolute difference between all pairs in a list
- Python - Make pair from two list such that elements are not same in pairs
- Program to find total unique duration from a list of intervals in Python
- Python – Check if elements index are equal for list elements
- How to count total number of occurrences of an object in a Python list?
- Making List Values Equal in C++

Advertisements