Open In App

Python – Extracting Priority Elements in Tuple List

Sometimes, while working with Python Records, we can have a problem in which we need to perform extraction of all the priority elements from records, which usually occur as one of the binary element tuple. This kind of problem can have possible application in web development and gaming domains. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [(7, 1), (3, 2), (4, 6)] prior_list = [1, 3, 4] 
Output : [1, 3, 4] 



Input : test_list = [(7, 3), (3, 4), (1, 6)] prior_list = [1, 3, 4] 
Output : [3, 4, 1]

Method #1 : Using loop This is brute force approach to solve this problem. In this, we iterate each element of the priority list and check for individual tuple, filter out the matching element and append to list. 






# Python3 code to demonstrate working of
# Extracting Priority Elements in Tuple List
# loop
 
# initializing list
test_list = [(5, 1), (3, 4), (9, 7), (10, 6)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing Priority list
prior_list = [6, 4, 7, 1]
 
# Extracting Priority Elements in Tuple List
# loop
res = []
for sub in test_list:
    for val in prior_list:
        if val in sub:
            res.append(val)
 
# printing result
print("The extracted elements are : " + str(res))

Output : 
The original list is : [(5, 1), (3, 4), (9, 7), (10, 6)]
The extracted elements are : [1, 4, 7, 6]

Time complexity: O(nm), where n is the length of test_list and m is the length of prior_list.
Auxiliary Space: O(k), where k is the length of the resulting list res. The space used is proportional to the number of priority elements extracted from the test_list.

Method #2 : Using List comprehension + index() The combination of above functions can be used to solve this problem. In this, we perform the task of checking for required element from tuple using index() and priority comparison. 




# Python3 code to demonstrate working of
# Extracting Priority Elements in Tuple List
# Using List comprehension + <code>index()
 
# initializing list
test_list = [(7, 1), (6, 4), (4, 7), (1, 6)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing Priority list
prior_list = [6, 4, 7, 1]
 
# Extracting Priority Elements in Tuple List
# Using List comprehension + <code>index()
res = [sub[0] if prior_list.index(sub[0]) < prior_list.index(sub[1])
              else sub[1] for sub in test_list]
 
# printing result
print("The extracted elements are : " + str(res))

Output : 
The original list is : [(7, 1), (6, 4), (4, 7), (1, 6)]
The extracted elements are : [7, 6, 4, 6]

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

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

Method 3: Using dictionary

  1. Initialize an empty dictionary to store the priorities of each element in test_list.
  2. Loop through prior_list and assign the index of each element as its priority in the dictionary.
  3. Loop through test_list, and for each tuple, compare the priorities of the two elements using the dictionary.
  4. Append the higher-priority element to a new list res.
  5. Return res




test_list = [(7, 1), (6, 4), (4, 7), (1, 6)]
prior_list = [6, 4, 7, 1]
 
# Step 1
priority_dict = {}
 
# Step 2
for i, p in enumerate(prior_list):
    priority_dict[p] = i
 
# Step 3-5
res = []
for tup in test_list:
    if priority_dict[tup[0]] < priority_dict[tup[1]]:
        res.append(tup[0])
    else:
        res.append(tup[1])
 
print("The extracted elements are : " + str(res))

Output
The extracted elements are : [7, 6, 4, 6]

Time complexity: O(n), where n is the length of test_list.
Auxiliary space: O(m), where m is the length of prior_list for the dictionary used to store priorities.

Method 4: Using reduce():

Algorithm:

  1. Initialize an empty dictionary ‘priority_dict’.
  2. For each priority number in ‘prior_list’, add an entry in the dictionary where the priority number is the key and its index in ‘prior_list’ is the value.
  3. Initialize an empty list ‘res’.
  4. For each tuple in ‘test_list’, get the priority indices of its elements using the ‘priority_dict’.
  5. Append the element with the lower priority index to the ‘res’ list.
  6. Return the ‘res’ list as the extracted priority elements.




from functools import reduce
 
test_list = [(7, 1), (6, 4), (4, 7), (1, 6)]
prior_list = [6, 4, 7, 1]
# printing original list
print("The original list is : " + str(test_list))
priority_dict = {p:i for i,p in enumerate(prior_list)}
 
res = reduce(lambda acc, tup: acc + [tup[0]] if priority_dict[tup[0]] < priority_dict[tup[1]] else acc + [tup[1]], test_list, [])
 
print("The extracted elements are : " + str(res))
 
#This code is contributed by Pushpa

Output
The original list is : [(7, 1), (6, 4), (4, 7), (1, 6)]
The extracted elements are : [7, 6, 4, 6]

Time Complexity: O(n), where n is the length of ‘test_list’. The time complexity of creating the dictionary is O(p), where p is the length of ‘prior_list’. However, since p is constant and small compared to n, we can consider the time complexity as O(n).

Space Complexity: O(p), where p is the length of ‘prior_list’. We need to store the indices of each priority number in the dictionary. Again, since p is constant and small compared to n, we can consider the space complexity as O(1).


Article Tags :