- 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 Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Python program to count Bidirectional Tuple Pairs
When it is required to count the number of bidirectional tuple pairs in a list of tuples, the list can be iterated over using nested loops, and the ‘AND’ operation is performed on the first element and result of equality between first and second element.
Below is a demonstration of the same −
my_list = [(45, 67), (11, 23), (67, 45), (23, 11), (0, 9), (67, 45)] print("The list is : ") print(my_list) my_result = 0 for idx in range(0, len(my_list)): for iidx in range(idx + 1, len(my_list)): if my_list[iidx] == my_list[idx] and my_list[idx] == my_list[iidx]: my_result += 1 print("The count of bidirectional pairs are : ") print(my_result)
The list is : [(45, 67), (11, 23), (67, 45), (23, 11), (0, 9), (67, 45)] The count of bidirectional pairs are : 3
A list of tuples is defined, and is displayed on the console.
A result variable is assigned to 0.
The list is iterated over twice.
The ‘AND’ operation is performed between two elements.
The first element and the result of equality check between second and first elements.
Now, the result variable is incremented.
This result is displayed on the console.
- Related Articles
- Python program to count pairs for consecutive elements
- Python program to count the pairs of reverse strings
- Program to count nice pairs in an array in Python
- Python program to count occurrences of an element in a tuple
- Python program to unique keys count for Value in Tuple List
- Program to count pairs with XOR in a range in Python
- Find Maximum difference between tuple pairs in Python
- Python - Split list into all possible tuple pairs
- Program to count number of fraction pairs whose sum is 1 in python
- Program to count index pairs for which array elements are same in Python
- Python Program to create a Tuple using tuple literal
- Python program to count the elements in a list until an element is a Tuple?
- Program to count indices pairs for which elements sum is power of 2 in Python
- Count the elements till first tuple in Python