Python | Preceding element tuples in list
Last Updated :
06 Apr, 2023
Sometimes, while working with Python list, we can have a problem in which we need to construct tuples, with the preceding element, whenever that element matches a particular condition. This can have potential application in day-day programming. Let’s discuss a way in which this task can be performed.
Method #1: Using for loop
Python3
test_list = [ 1 , 4 , 'gfg' , 7 , 8 , 'gfg' , 9 , 'gfg' ]
print ( "The original list is : " + str (test_list))
ele = 'gfg'
res = []
for i in range ( 0 , len (test_list)):
if (test_list[i] = = ele):
res.append((test_list[i - 1 ],test_list[i]))
print ( "Tuple list with desired Preceding elements " + str (res))
|
Output
The original list is : [1, 4, 'gfg', 7, 8, 'gfg', 9, 'gfg']
Tuple list with desired Preceding elements [(4, 'gfg'), (8, 'gfg'), (9, 'gfg')]
Time Complexity: O(n*n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method 2: Using zip() + list comprehension
This task can be performed using a combination of above functionalities. In this, the zip() performs the task of construction of tuples and the catering of condition matching and iteration is handled by list comprehension.
Python3
test_list = [ 1 , 4 , 'gfg' , 7 , 8 , 'gfg' , 9 , 'gfg' ]
print ( "The original list is : " + str (test_list))
ele = 'gfg'
res = [(x, y) for x, y in zip (test_list, test_list[ 1 : ]) if y = = ele]
print ( "Tuple list with desired Preceding elements " + str (res))
|
Output
The original list is : [1, 4, 'gfg', 7, 8, 'gfg', 9, 'gfg']
Tuple list with desired Preceding elements [(4, 'gfg'), (8, 'gfg'), (9, 'gfg')]
Time Complexity: O(N), where N is the length of the input list test_list.
Auxiliary Space: O(N)
Method 3: Using enumerate
We loop through the original_list using enumerate() function which returns the index and value of each element.
For each element, we check if it is equal to ‘gfg’ and its index is greater than 0. If it is true, we add a tuple containing the preceding element and the current element to the tuple_list.We use list comprehension to create the tuple_list.
Finally, we print both the original and tuple list.
Python3
test_list1 = [( 1 , 7 ), ( 6 , 7 ), ( 9 , 100 ), ( 4 , 21 )]
test_list2 = [( 1 , 3 ), ( 2 , 1 ), ( 9 , 7 ), ( 2 , 17 )]
def cross_pairing(test_list1, test_list2):
result = []
for tup1 in test_list1:
for tup2 in test_list2:
if tup1[ 0 ] = = tup2[ 0 ]:
result.append((tup1[ 1 ], tup2[ 1 ]))
return result
print (cross_pairing(test_list1, test_list2))
|
Output
Original List : [1, 4, 'gfg', 7, 8, 'gfg', 9, 'gfg']
Tuple List with desired Preceding elements : [(4, 'gfg'), (8, 'gfg'), (9, 'gfg')]
Time complexity: O(n^2), where n is the length of the longer input list. This is because the code iterates over each tuple in test_list1 and each tuple in test_list2, resulting in nested loops that perform n^2 comparisons.
Auxiliary Space: O(n^2), since the output list result can contain up to n^2 cross pairs. Additionally, the memory usage required for storing the input lists is O(n), since each list contains n tuples. Therefore, the total space complexity is O(n^2 + n), which can be simplified to O(n^2) since the n^2 term dominates the space requirements.
Method 4:Using reduce:
Algorithm:
1.Initialize a list test_list and a variable ele.
2.Use a list comprehension to generate a list of tuples, where each tuple contains two consecutive elements of test_list.
3.Filter the list of tuples using an if statement to only include tuples where the second element is equal to ele.
4.Use the reduce function to apply a lambda function to the filtered list of tuples. The lambda function returns a new 5.list that contains all the tuples in the previous list, plus a new tuple that contains the first and second element of the current tuple.
6.The result of the reduce function is a list of tuples containing the desired preceding elements.
Python3
from functools import reduce
test_list = [ 1 , 4 , 'gfg' , 7 , 8 , 'gfg' , 9 , 'gfg' ]
print ( "The original list is : " + str (test_list))
ele = 'gfg'
res = reduce ( lambda x, y: x + [(y[ 0 ], y[ 1 ])], [(test_list[i], test_list[i + 1 ]) for i in range ( len (test_list) - 1 ) if test_list[i + 1 ] = = ele], [])
print ( "Tuple list with desired Preceding elements: " , res)
|
Output
The original list is : [1, 4, 'gfg', 7, 8, 'gfg', 9, 'gfg']
Tuple list with desired Preceding elements: [(4, 'gfg'), (8, 'gfg'), (9, 'gfg')]
Time complexity: O(n), where n is the length of test_list.
Auxiliary Space: O(n), since the list of tuples generated by the list comprehension could potentially be as large as test_list. However, since the reduce function operates on the filtered list of tuples, the actual space complexity of the code may be smaller depending on how many tuples are filtered out by the if statement.
Share your thoughts in the comments
Please Login to comment...