Python - Double each consecutive duplicate
Last Updated :
04 Feb, 2023
Sometimes, while working with data, we can have a problem in which we need to perform double of element on each consecutive occurrence of a duplicate. This is very specific problem, but solution to this can prove to be very handy. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop This is brute force way to perform this task. In this, we iterate each element and when we find duplicate we store in dictionary and perform its double subsequently.
Python3
# Python3 code to demonstrate
# Double each consecutive duplicate
# using loop
# Initializing list
test_list = [1, 2, 4, 2, 4, 1, 2]
# printing original list
print("The original list is : " + str(test_list))
# Double each consecutive duplicate
# using loop
temp = {}
res = []
for ele in test_list:
temp[ele] = temp1 = temp.get(ele, 0) + ele
res.append(temp1)
# printing result
print ("The list after manipulation is : " + str(res))
Output : The original list is : [1, 2, 4, 2, 4, 1, 2]
The list after manipulation is : [1, 2, 4, 4, 8, 2, 6]
Time Complexity: O(n), where n is the number of elements in the list "test_list". This is because the code iterates through the list "test_list" once and performs a constant amount of work for each element.
Auxiliary Space Complexity: O(n), where n is the number of elements in the list "test_list". This is because the code uses a dictionary "temp" to store the frequency of elements in the list, which can take up to O(n) space in the worst-case scenario.
Method #2 : Using defaultdict() + loop This method performs this task in similar way as above. The only difference is a step is reduced by using defaultdict() as it pre initializes the list.
Python3
# Python3 code to demonstrate
# Double each consecutive duplicate
# using loop + defaultdict()
from collections import defaultdict
# Initializing list
test_list = [1, 2, 4, 2, 4, 1, 2]
# printing original list
print("The original list is : " + str(test_list))
# Double each consecutive duplicate
# using loop + defaultdict()
temp = defaultdict(int)
res = []
for ele in test_list:
temp[ele] += ele
res.append(temp[ele])
# printing result
print ("The list after manipulation is : " + str(res))
Output : The original list is : [1, 2, 4, 2, 4, 1, 2]
The list after manipulation is : [1, 2, 4, 4, 8, 2, 6]
Time complexity: O(n), where n is the number of elements in the test_list. This is because we are looping through the test_list once, which takes O(n) time.
Auxiliary space: O(n), because we are using a defaultdict of size n to store the elements.
Method #3 : Using for loop and count() method
Python3
# Python3 code to demonstrate
# Double each consecutive duplicate
# using loop
# Initializing list
test_list = [1, 2, 4, 2, 4, 1, 2]
# printing original list
print("The original list is : " + str(test_list))
# Double each consecutive duplicate
# using loop
res = []
for i in range(0,len(test_list)):
x=test_list[:i+1].count(test_list[i])
y=test_list[i]
res.append(x*y)
# printing result
print ("The list after manipulation is : " + str(res))
OutputThe original list is : [1, 2, 4, 2, 4, 1, 2]
The list after manipulation is : [1, 2, 4, 4, 8, 2, 6]
Time Complexity : O(N)
Auxiliary Space : O(N)
Similar Reads
Python - Consecutive identical elements count Given the elements list, get all the elements that have an identical element as the next element. Input : test_list = [4, 5, 5, 5, 5, 6, 6, 7, 8, 2, 2, 10] Output : 3 Explanation : 5, 6 and 2 has identical element as their next element. Input : test_list = [4, 5, 5, 5, 5, 6, 6, 7, 8, 2, 3, 10] Outpu
5 min read
Python - Remove alternate consecutive duplicates Given list of elements, remove alternate consecutive duplicates of elements. Input : test_list = [5, 5, 5, 5, 6, 6] Output : [5, 5, 6] Explanation : Alternate occ. of 5 and 6 are removed. Input : test_list = [5, 5, 5, 5] Output : [5, 5] Explanation : Alternate occ. of 5 are removed. Method : Using l
2 min read
Python - Multiply Consecutive elements in list While working with python, we usually come by many problems that we need to solve in day-day and in development. Specially in development, small tasks of python are desired to be performed in just one line. We discuss some ways to compute a list consisting of elements that are successive product in
7 min read
Python - Similar Consecutive elements frequency Sometimes, while working with Python, we can have a problem in which we have to find the occurrences of elements that are present consecutively. This problem have usage in school programming and data engineering. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop
5 min read
Python - Minimum identical consecutive Subarray Sometimes, while working with Python lists or in competitive programming setup, we can come across a subproblem in which we need to get an element that has the minimum consecutive occurrence. The knowledge of solution to it can be of great help and can be employed whenever required. Letâs discuss a
3 min read
Python - Reorder for consecutive elements Given a List perform reordering to get similar elements in consecution. Input : test_list = [4, 7, 5, 4, 1, 4, 1, 6, 7, 5] Output : [4, 4, 4, 7, 7, 5, 5, 1, 1, 6] Explanation : All similar elements are assigned to be consecutive. Input : test_list = [4, 7, 5, 1, 4, 1, 6, 7, 5] Output : [4, 4, 7, 7,
4 min read
Python - Remove Columns of Duplicate Elements Given a Matrix, write a Python program to remove whole column if duplicate occurs in any row. Examples: Input : test_list = [[4, 3, 5, 2, 3], [6, 4, 2, 1, 1], [4, 3, 9, 3, 9], [5, 4, 3, 2, 1]] Output : [[4, 3, 5], [6, 4, 2], [4, 3, 9], [5, 4, 3]] Explanation : 1 has duplicate as next element hence 5
3 min read
Python - Consecutive Repetition of Characters Sometimes, while working with character lists we can have a problem in which we need to perform consecutive repetition of characters. This can have applications in many domains. Let us discuss certain ways in which this task can be performed. Method #1: Using list comprehension This is one of the
5 min read
Python - Pair iteration in list Pair iteration involves accessing consecutive or specific pairs of elements from a list. It is a common task particularly in scenarios such as comparing neighboring elements, creating tuple pairs, or analyzing sequential data. Python provides several ways to iterate through pairs efficiently ranging
2 min read
Python - Double Each List Element We are given a list we need to double each element in given list. For example, we are having a list a = [1, 2, 3, 4, 5] we need to double list so that output should be [2, 4, 6, 8, 10].Using List ComprehensionList comprehension allows us to create a new list by iterating over an existing list and ap
3 min read