Python - Extracting Priority Elements in Tuple List
Last Updated :
11 May, 2023
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
# 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
# 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
- Initialize an empty dictionary to store the priorities of each element in test_list.
- Loop through prior_list and assign the index of each element as its priority in the dictionary.
- Loop through test_list, and for each tuple, compare the priorities of the two elements using the dictionary.
- Append the higher-priority element to a new list res.
- Return res
Python3
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))
OutputThe 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:
- Initialize an empty dictionary 'priority_dict'.
- 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.
- Initialize an empty list 'res'.
- For each tuple in 'test_list', get the priority indices of its elements using the 'priority_dict'.
- Append the element with the lower priority index to the 'res' list.
- Return the 'res' list as the extracted priority elements.
Python3
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
OutputThe 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).
Similar Reads
Python - Extract records if Kth elements not in List
Given list of tuples, task is to extract all the tuples where Kth index elements are not present in argument list. Input : test_list = [(5, 3), (7, 4), (1, 3), (7, 8), (0, 6)], arg_list = [6, 8, 8], K = 1 Output : [(5, 3), (7, 4), (1, 3)] Explanation : All the elements which have either 6 or 8 at 1s
4 min read
Python - Extract Kth element of every Nth tuple in List
Given list of tuples, extract Kth column element of every Nth tuple. Input :test_list = [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8), (6, 4, 7), (2, 5, 7), (1, 9, 10), (3, 5, 7)], K = 2, N = 3 Output : [3, 8, 10] Explanation : From 0th, 3rd, and 6th tuple, 2nd elements are 3, 8, 10. Input :test_list
8 min read
Python - Extract Even elements in Nested Mixed Tuple
Sometimes, while working with Python tuples, we can have a problem in which we need to get all the even elements from the tuple. The tuples can be nested or mixed. This kind of problem can occur in data domains. Let's discuss certain ways in which this task can be performed. Input : test_tuple = (5,
4 min read
Python | Minimum element in tuple list
Sometimes, while working with data in form of records, we can have a problem in which we need to find the minimum element of all the records received. This is a very common application that can occur in Data Science domain. Let's discuss certain ways in which this task can be performed. Method #1 :
5 min read
Python - Extract Elements from Ranges in List
We are given a list and list containing tuples we need to extract element from ranges in tuples list. For example, n = [10, 20, 30, 40, 50, 60, 70, 80, 90] and r = [(1, 3), (5, 7)] (ranges) we need to extract elements so that output should be [[20, 30, 40], [60, 70, 80]].Using List ComprehensionList
3 min read
Python - Rear element extraction from list of tuples records
While working with tuples, we store different data as different tuple elements. Sometimes, there is a need to print specific information from the tuple like rear index. For instance, a piece of code would want just names to be printed on all the student data. Let's discuss certain ways in which one
8 min read
Python | Maximum element in tuple list
Sometimes, while working with data in form of records, we can have a problem in which we need to find the maximum element of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1: U
6 min read
Python - Check if element is present in tuple
We are given a tuple and our task is to find whether given element is present in tuple or not. For example x = (1, 2, 3, 4, 5) and we need to find if 3 is present in tuple so that in this case resultant output should be True.Using in Operatorin operator checks if an element is present in a tuple by
2 min read
Python - Test if greater than preceding element in Tuple List
Given list of tuples, check if preceding element is smaller than the current element for each element in Tuple list. Input : test_list = [(5, 1), (4, 9), (3, 5)] Output : [[False, False], [False, True], [False, True]] Explanation : First element always being False, Next element is checked for greate
9 min read
Python - Extract tuples with elements in Range
Given list of tuples, extract tuples having elements in range. Input : test_list = [(4, 5, 7), (5, 6), (3, 8, 10 ), (4, 10)], strt, end = 5, 6 Output : [(5, 6)] Explanation : Only 1 tuple lies in range of 5-6. Input : test_list = [(4, 5, 7), (5, 6), (3, 8, 10 ), (4, 10)], strt, end = 1, 10 Output :
4 min read