Open In App

Python – Extract Similar pairs from List

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python lists, we can have a problem in which we need to perform extraction of all the elements pairs in list. This kind of problem can have application in domains such as web development and day-day programming. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [1, 2, 3, 4] 
Output : [] 

Input : test_list = [2, 2, 2, 2, 3, 3, 4] 
Output : [(2, 2), (2, 2), (3, 3)]

Method #1 : Using Counter() + list comprehension The combination of above functions can be used to solve this problem. In this, frequencies are extracted using Counter() and pairs construction is done used list comprehension. 

Python3




# Python3 code to demonstrate working of
# Extract Similar pairs from List
# Using Counter() + list comprehension
from collections import Counter
 
# initializing list
test_list = [4, 6, 7, 4, 2, 6, 2, 8]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Extract Similar pairs from List
# Using Counter() + list comprehension
res = [(key, key) for key, val in Counter(test_list).items()
                                  for _ in range(val // 2)]
 
# printing result
print("The records after pairing : " + str(res))


Output : 

The original list is : [4, 6, 7, 4, 2, 6, 2, 8]
The records after pairing : [(4, 4), (6, 6), (2, 2)]

Time Complexity: O(n*n) where n is the number of elements in the in the list “test_list”. The Counter() + list comprehension is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the in the list “test_list”.

Method #2 : Using fromkeys() + list comprehension This is yet another way in which this task can be performed. In this, we perform the task of Counter() using fromkeys() and get() of dictionary, i.e getting frequencies. 

Python3




# Python3 code to demonstrate working of
# Extract Similar pairs from List
# Using fromkeys() + list comprehension
 
# initializing list
test_list = [4, 6, 7, 4, 2, 6, 2, 8]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Extract Similar pairs from List
# Using fromkeys() + list comprehension
temp = dict.fromkeys(test_list, 0)
for key in test_list:
    temp[key] += 1
res = [(key, key) for key, val in temp.items() for _ in range(val // 2)]
 
# printing result
print("The records after pairing : " + str(res))


Output : 

The original list is : [4, 6, 7, 4, 2, 6, 2, 8]
The records after pairing : [(4, 4), (6, 6), (2, 2)]

Method 3 :  Use a set 

Python3




# Python3 code to demonstrate working of
# Extract Similar pairs from List
# Using a set
 
# initializing list
test_list = [4, 6, 7, 4, 2, 6, 2, 8]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Extract Similar pairs from List
# Using a set
odd_freq = set()
res = []
for elem in test_list:
    if elem in odd_freq:
        res.append((elem, elem))
        odd_freq.remove(elem)
    else:
        odd_freq.add(elem)
 
# printing result
print("The records after pairing : " + str(res))


Output

The original list is : [4, 6, 7, 4, 2, 6, 2, 8]
The records after pairing : [(4, 4), (6, 6), (2, 2)]

The time complexity of this solution is O(n), where n is the length of test_list, since we iterate over the list once. The auxiliary space is O(n) as well, since the set odd_freq can contain up to n/2 elements in the worst case 

Method #4: Using dictionary

  • Create an empty dictionary freq_dict to keep track of the frequency of each element in the list.
  • Iterate through the elements of the input list test_list.
  • If an element is already present in freq_dict, then add it to the output list res as a pair (elem, elem) and decrement its frequency in freq_dict.
  • If an element is not present in freq_dict, then initialize its frequency to 1.
  • Finally, return the output list res.

Python3




# Python3 code to demonstrate working of
# Extract Similar pairs from List
# Using dictionary
 
# initializing list
test_list = [4, 6, 7, 4, 2, 6, 2, 8]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Extract Similar pairs from List
# Using dictionary
freq_dict = {}
res = []
for elem in test_list:
    if elem in freq_dict and freq_dict[elem] > 0:
        res.append((elem, elem))
        freq_dict[elem] -= 1
    else:
        freq_dict[elem] = 1
 
# printing result
print("The records after pairing : " + str(res))


Output

The original list is : [4, 6, 7, 4, 2, 6, 2, 8]
The records after pairing : [(4, 4), (6, 6), (2, 2)]

Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(n), where n is the length of the input list test_list.



Last Updated : 16 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads