Open In App

Python – Successive element pairing

Improve
Improve
Like Article
Like
Save
Share
Report

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 list
test_list = [1, 4, 'gfg', 7, 8, 'gfg', 9, 'gfg', 10]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize ele
ele = 'gfg'
 
# Successive element pairing
# using zip() + list comprehension
res = [(x, y) for x, y in zip(test_list, test_list[1 : ]) if x == ele]
 
# printing result
print("Tuple list with desired Successive elements " + str(res))


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: 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 pairing
test_list = [1, 4, 'gfg', 7, 8, 'gfg', 9, 'gfg', 10]
ele = 'gfg'
# Successive element pairing
res = [(test_list[i], test_list[i+1])
       for i in range(len(test_list) - 1) if test_list[i] == ele]
# printing result
print("Tuple list with desired Successive elements " + str(res))
 
# This code is contributed by Edula Vinay Kumar Reddy


Output

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 list
test_list = [1, 4, 'gfg', 7, 8, 'gfg', 9, 'gfg', 10]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize ele
ele = 'gfg'
 
# Successive element pairing
# using filter() + lambda function
res = list(filter(lambda x: x[0] == ele, zip(test_list, test_list[1:])))
 
# printing result
print("Tuple list with desired Successive elements " + str(res))


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:  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:

  1. Initialize the list and the element to be paired.
  2. Use filter() method with lambda function to filter elements in pairs from the given list.
  3. Store the resulting pairs in a list.
  4. Print the final list.

Python3




import numpy as np
 
# initialize list
test_list = [1, 4, 'gfg', 7, 8, 'gfg', 9, 'gfg', 10]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize ele
ele = 'gfg'
 
# convert list to numpy array
arr = np.array(test_list)
 
# get indices where element is 'gfg'
idx = np.where(arr == ele)[0]
 
# filter the successive elements
res = [(test_list[i], test_list[i+1]) for i in idx if i < len(test_list)-1]
 
# printing result
print("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.



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