Open In App

Python – Consecutive Pairs comma removal

Last Updated : 04 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working amongst lists, we can have a problem in which we need to pair up elements from two lists. In this, we can have pairs in which we find that there is comma that is printed in tuple and we wish to avoid it usually. Lets discuss certain ways in which this task can be performed. 

Method #1 : Using zip() + list comprehension + replace() 

The combination of above functionalities can be used to perform this task. In this, we join lists using zip() and task of removal of comma and joining is performed using replace(). 

Approach :

  1. Two lists test_list1 and test_list2 are initialized with five integer values each.
  2. The original contents of test_list1 and test_list2 are printed using the print() function and string concatenation.
  3. A new list is created using the zip() function to pair the elements of the two lists together. The resulting list contains tuples of pairs from the two original lists.
  4. The str() function is used to convert the list of tuples into a string.
  5. The replace() function is called on the resulting string to remove the commas between each tuple and replace them with a space. This results in the removal of consecutive pairs duplication.
  6. The resulting string is assigned to a variable res.
  7. The final output is printed using the print() function and string concatenation. It displays the combined list after consecutive comma removal.

Example:

Python3




# Python3 code to demonstrate
# Consecutive Pairs Duplication Removal
# using list comprehension + zip() + replace()
 
# Initializing lists
test_list1 = [1, 2, 3, 4, 5]
test_list2 = [2, 3, 4, 5, 6]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Consecutive Pairs Duplication Removal
# using list comprehension + zip() + replace()
res = str(list(zip(test_list1, test_list2))).replace('), (', ') (')
 
# printing result
print("The combined list after consecutive comma removal : " + str(res))


Output : 

The original list 1 is : [1, 2, 3, 4, 5]
The original list 2 is : [2, 3, 4, 5, 6]
The combined list after consecutive comma removal : [(1, 2) (2, 3) (3, 4) (4, 5) (5, 6)]

Time complexity: O(n)
Auxiliary space: O(n)

Method #2 : Using map() + list comprehension + zip() This is yet another way in which this task can be performed. In this we perform the task performed using replace() with map(). 

Python3




# Python3 code to demonstrate
# Consecutive Pairs Duplication Removal
# using list comprehension + zip() + map()
 
# Initializing lists
test_list1 = [1, 2, 3, 4, 5]
test_list2 = [2, 3, 4, 5, 6]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Consecutive Pairs Duplication Removal
# using list comprehension + zip() + map()
res = "[" + " ".join(map(str, zip(test_list1, test_list2))) + "]"
 
# printing result
print("The combined list after consecutive comma removal : " + str(res))


Output : 

The original list 1 is : [1, 2, 3, 4, 5]
The original list 2 is : [2, 3, 4, 5, 6]
The combined list after consecutive comma removal : [(1, 2) (2, 3) (3, 4) (4, 5) (5, 6)]

Time complexity: O(n)
Auxiliary space: O(n)

Method #3 : Using format() and eval()

This is yet another way in which this task can be performed. In this, we perform the task of joining the elements with join() and generator expression is used to perform this task.

Python3




# Python3 code to demonstrate
# Consecutive Pairs Duplication Removal
# using join() + generator expression
 
# Initializing lists
test_list1 = [1, 2, 3, 4, 5]
test_list2 = [2, 3, 4, 5, 6]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Consecutive Pairs Duplication Removal
# using join() + generator expression
res = eval("[" + ", ".join("({}, {})".format(*e)
                           for e in zip(test_list1, test_list2)) + "]")
 
# printing result
print("The combined list after consecutive comma removal : " + str(res))
# This code is contributed by Edula Vinay Kumar Reddy


Output

The original list 1 is : [1, 2, 3, 4, 5]
The original list 2 is : [2, 3, 4, 5, 6]
The combined list after consecutive comma removal : [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]

Time Complexity: O(m*n) where m and n are the lengths of the two given lists.
Auxiliary Space: O(m*n) for storing output list.

Method #4: Using a loop and appending to a new list

Initializes two lists test_list1 and test_list2, prints their original values, and then creates a new list res that contains pairs of consecutive elements from test_list1 and test_list2 using a loop. The final result is printed as res.

Python3




# Initializing lists
test_list1 = [1, 2, 3, 4, 5]
test_list2 = [2, 3, 4, 5, 6]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Consecutive Pairs Duplication Removal using a loop
res = []
for i in range(len(test_list1)):
    res.append((test_list1[i], test_list2[i]))
 
# printing result
print("The combined list after consecutive comma removal : " + str(res))


Output

The original list 1 is : [1, 2, 3, 4, 5]
The original list 2 is : [2, 3, 4, 5, 6]
The combined list after consecutive comma removal : [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]

Time complexity: O(n), where n is the length of the input lists test_list1 and test_list2. 
Auxiliary space: O(n), as we are creating a new list res of size n to store the pairs.

Method #5: Using itertools module

Use the itertools module in Python to solve the problem of removing consecutive duplicates from two lists. The groupby() function in itertools groups the elements of a list based on a given key function. We can use this function to group the elements of the zipped list based on the first element of each tuple. Then, we can take the first element of each group and create a new list.

  • Initialize two lists: test_list1 and test_list2.
  • Print the original contents of both lists using the print() function.
  • Create a new list by zipping the elements of the two original lists together using the zip() function. This will create a list of tuples where the first element of each tuple is from test_list1 and the second element of each tuple is from test_list2.
  • Use the groupby() function from the itertools module to group the tuples in the zipped list based on their first element.
  • Create a new list by iterating over each group returned by the groupby() function and appending the first tuple of each group to the new list. This will remove consecutive duplicates from the original lists.
  • Print the final list that contains non-consecutive duplicate elements.

Python3




from itertools import groupby
 
# Initializing lists
test_list1 = [1, 2, 3, 4, 5]
test_list2 = [2, 3, 4, 5, 6]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Consecutive Pairs Duplication Removal
# using itertools.groupby()
zipped_list = list(zip(test_list1, test_list2))
grouped_list = [list(group)[0]
                for key, group in groupby(zipped_list, lambda x: x[0])]
 
# printing result
print("The combined list after consecutive comma removal : " + str(grouped_list))


Output

The original list 1 is : [1, 2, 3, 4, 5]
The original list 2 is : [2, 3, 4, 5, 6]
The combined list after consecutive comma removal : [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]

Time complexity: O(n) (where n is the length of the input lists)
Auxiliary space: O(n) (where n is the length of the input lists)

Method 6: Using a numpy

Step-by-step approach :

  1. Import numpy library using import numpy as np.
  2. Initialize two lists test_list1 and test_list2 with some values.
  3. Convert the two lists to numpy arrays using arr1 = np.array(test_list1) and arr2 = np.array(test_list2).
  4. Combine the arrays using numpy’s column_stack() function, which stacks 1-D arrays as columns into a 2-D array. Here, combine arr1 and arr2 using res = np.column_stack((arr1, arr2)).
  5. Convert the resulting numpy array res back to a list of tuples using res = list(map(tuple, res)).
  6. Print the resulting list.

Below is the implementation of the above approach:

Python3




# Importing numpy library
import numpy as np
 
# Initializing lists
test_list1 = [1, 2, 3, 4, 5]
test_list2 = [2, 3, 4, 5, 6]
 
# Converting lists to numpy arrays
arr1 = np.array(test_list1)
arr2 = np.array(test_list2)
 
# Combining the arrays using numpy's column_stack() function
res = np.column_stack((arr1, arr2))
 
# Converting the resulting numpy array back to a list of tuples
res = list(map(tuple, res))
 
# printing result
print("The combined list after consecutive comma removal : " + str(res))


Output:

The combined list after consecutive comma removal : [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]

Time complexity: O(n)
Auxiliary space: O(n)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads