Sometimes, while working with Python list, we can have a problem in which we need to construct tuples, with the succeeding 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 : Using zip() + list comprehension
This task can be performed using the 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
# Python3 code to demonstrate working of# Successive element pairing# using zip() + list comprehension# initialize listtest_list = [1, 4, 'gfg', 7, 8, 'gfg', 9, 'gfg', 10]# printing original listprint("The original list is : " + str(test_list))# initialize ele ele = 'gfg'# Successive element pairing# using zip() + list comprehensionres = [(x, y) for x, y in zip(test_list, test_list[1 : ]) if x == ele]# printing resultprint("Tuple list with desired Successive elements " + str(res)) |
The original list is : [1, 4, 'gfg', 7, 8, 'gfg', 9, 'gfg', 10]
Tuple list with desired Successive elements [('gfg', 7), ('gfg', 9), ('gfg', 10)]
Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Approach 2: Using range()
The below approach uses list comprehension to loop through the given list and store the tuple in result list if the element matches the given element.
Python3
# Python3 code to demonstrate working of# Successive element pairingtest_list = [1, 4, 'gfg', 7, 8, 'gfg', 9, 'gfg', 10]ele = 'gfg'# Successive element pairingres = [(test_list[i], test_list[i+1]) for i in range(len(test_list) - 1) if test_list[i] == ele]# printing resultprint("Tuple list with desired Successive elements " + str(res))# This code is contributed by Edula Vinay Kumar Reddy |
Tuple list with desired Successive elements [('gfg', 7), ('gfg', 9), ('gfg', 10)]
Time Complexity : O(n)
Auxiliary Space: O(n)
Approach 3: Using the filter() method
The filter() function filters out elements from a sequence based on a given condition (in this case, lambda x: x[0] == ele, which checks if the first element of the tuple is equal to ele. zip() function creates an iterator that aggregates elements from two or more iterables. The resulting iterator returns tuples containing elements from each iterable. Finally, the list() function converts the filtered iterator into a list.
Python3
# initialize listtest_list = [1, 4, 'gfg', 7, 8, 'gfg', 9, 'gfg', 10]# printing original listprint("The original list is : " + str(test_list))# initialize eleele = 'gfg'# Successive element pairing# using filter() + lambda functionres = list(filter(lambda x: x[0] == ele, zip(test_list, test_list[1:])))# printing resultprint("Tuple list with desired Successive elements " + str(res)) |
The original list is : [1, 4, 'gfg', 7, 8, 'gfg', 9, 'gfg', 10]
Tuple list with desired Successive elements [('gfg', 7), ('gfg', 9), ('gfg', 10)]
Time Complexity: O(n).
Auxiliary Space: O(k), as we are creating a new list res to store the pairs of successive elements in test_list that start with ele.
Approach 4: Using numpy:
Algorithm:
- Initialize the list and the element to be paired.
- Use filter() method with lambda function to filter elements in pairs from the given list.
- Store the resulting pairs in a list.
- Print the final list.
Python3
import numpy as np# initialize listtest_list = [1, 4, 'gfg', 7, 8, 'gfg', 9, 'gfg', 10]# printing original listprint("The original list is : " + str(test_list))# initialize eleele = 'gfg'# convert list to numpy arrayarr = np.array(test_list)# get indices where element is 'gfg'idx = np.where(arr == ele)[0]# filter the successive elementsres = [(test_list[i], test_list[i+1]) for i in idx if i < len(test_list)-1]# printing resultprint("Tuple list with desired Successive elements " + str(res))# This code is contributed by Jyothi pinjala |
Output:
The original list is : [1, 4, 'gfg', 7, 8, 'gfg', 9, 'gfg', 10]
Tuple list with desired Successive elements [('gfg', 7), ('gfg', 9), ('gfg', 10)]
Time Complexity: The time complexity of this code will be O(n), where n is the number of elements in the list.
Auxiliary Space: The space complexity of this code will be O(k), where k is the number of pairs in the resulting list.
