Skip to content
Related Articles

Related Articles

Improve Article

Python – Substitute K for first occurrence of elements

  • Last Updated : 03 Jul, 2020

Sometimes, while working with Python data, we can have a problem in which, we need to perform 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 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))
Output :



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]

 

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 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))
Output :
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]

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :