Sometimes, while working with Python data, we can have a problem in which, we need to perform a substitution to the first occurrence of each element in list. This type of problem can have application in various domains such as web development. Let’s discuss certain ways in which this task can be performed.
Input : test_list = [4, 3, 3], K = 10
Output : [10, 10, 3]Input : test_list = [4, 3, 7], K = 8
Output : [8, 8, 8]
Method #1: Using loop This is brute way to solve this problem. In this, we run a loop for each element in list and store already occurred element for lookup, and accordingly assign K.
Python3
# Python3 code to demonstrate working of # Substitute K for first occurrence of elements # Using loop # initializing list test_list = [ 4 , 3 , 3 , 7 , 8 , 7 , 4 , 6 , 3 ] # printing original list print ("The original list is : " + str (test_list)) # initializing K K = 10 # Substitute K for first occurrence of elements # Using loop lookp = set () res = [] for ele in test_list: if ele not in lookp: lookp.add(ele) res.append(K) else : res.append(ele) # printing result print (" List after Substitution : " + str (res)) |
The original list is : [4, 3, 3, 7, 8, 7, 4, 6, 3] List after Substitution : [10, 10, 3, 10, 10, 7, 4, 10, 3]
Time complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(n), where n is the length of the input list. This is because we create a set to keep track of the elements we have already seen, and a new list to store the updated elements. The size of both of these data structures is proportional to the size of the input list.
Method #2 : Using defaultdict() + next() + count + list comprehension The combination of above functions provide shorthand to solve this problem. In this, we perform the task of checking for 1st occurrence using count and next() returns if element its 1st occurrence and creates boolean list comprising of 1s for 1st occurrence and 0 for duplicates. These are converted to desired result using list comprehension.
Python3
# Python3 code to demonstrate working of # Substitute K for first occurrence of elements # Using defaultdict() + next() + count + list comprehension from itertools import count from collections import defaultdict # initializing list test_list = [ 4 , 3 , 3 , 7 , 8 , 7 , 4 , 6 , 3 ] # printing original list print ("The original list is : " + str (test_list)) # initializing K K = 10 # Substitute K for first occurrence of elements # Using defaultdict() + next() + count + list comprehension freq = defaultdict(count) temp = [ int ( next (freq[val]) = = 0 ) for val in test_list] res = [K if ele else test_list[idx] for idx, ele in enumerate (temp)] # printing result print (" List after Substitution : " + str (res)) |
The original list is : [4, 3, 3, 7, 8, 7, 4, 6, 3] List after Substitution : [10, 10, 3, 10, 10, 7, 4, 10, 3]
Time complexity: O(n), where n is the length of the input list ‘test_list’.
Auxiliary space: O(n), where n is the length of the input list ‘test_list’.
Method #3: Using List Index
use the index of the first occurrence of each element in the list. We can loop through the list and use the index() method to find the index of the first occurrence of each element. If the index is equal to the current index in the loop, we substitute the element with K, otherwise we leave it unchanged.
Python3
# Python3 code to demonstrate working of # Substitute K for first occurrence of elements # Using list index # initializing list test_list = [ 4 , 3 , 3 , 7 , 8 , 7 , 4 , 6 , 3 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 10 # Substitute K for first occurrence of elements # Using list index for i in range ( len (test_list)): if i = = test_list.index(test_list[i]): test_list[i] = K # printing result print ( "List after Substitution : " + str (test_list)) |
The original list is : [4, 3, 3, 7, 8, 7, 4, 6, 3] List after Substitution : [10, 10, 10, 10, 10, 10, 10, 10, 10]
This method has a time complexity of O(n^2) because we are using the index() method which has a time complexity of O(n) inside a loop that runs n times.
This method has a space complexity of O(1) because we are not using any extra space to store the elements of the list.
Method #4 : Using count() method
Approach
- Replace the first occurrence of element by K (checking first occurrence using count())
- If not first occurrence append the element as it is to output list
- Display output list
Python3
# Python3 code to demonstrate working of # Substitute K for first occurrence of elements # Using loop # initializing list test_list = [ 4 , 3 , 3 , 7 , 8 , 7 , 4 , 6 , 3 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 10 # Substitute K for first occurrence of elements # Using loop res = [] for i in range ( 0 , len (test_list)): if (test_list[:i + 1 ].count(test_list[i]) = = 1 ): res.append(K) else : res.append(test_list[i]) # printing result print ( "List after Substitution : " + str (res)) |
The original list is : [4, 3, 3, 7, 8, 7, 4, 6, 3] List after Substitution : [10, 10, 3, 10, 10, 7, 4, 10, 3]
Time Complexity : O(N)
Auxiliary Space : O(N)
Method #5 : Using operator.countOf() method
Approach
- Replace the first occurrence of element by K (checking first occurrence using operator.countOf())
- If not first occurrence append the element as it is to output list
- Display output list
Python3
# Python3 code to demonstrate working of # Substitute K for first occurrence of elements # Using loop # initializing list test_list = [ 4 , 3 , 3 , 7 , 8 , 7 , 4 , 6 , 3 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 10 # Substitute K for first occurrence of elements # Using loop res = [] import operator for i in range ( 0 , len (test_list)): if (operator.countOf(test_list[:i + 1 ],test_list[i]) = = 1 ): res.append(K) else : res.append(test_list[i]) # printing result print ( "List after Substitution : " + str (res)) |
The original list is : [4, 3, 3, 7, 8, 7, 4, 6, 3] List after Substitution : [10, 10, 3, 10, 10, 7, 4, 10, 3]
Time Complexity : O(N) N – length of test_list
Auxiliary Space : O(N) N – length of output list(res)