Sunday, December 29, 2024
Google search engine
HomeLanguagesPython – Summation after elements removal

Python – Summation after elements removal

Sometimes we need to perform the operation of removing all the items from the lists that are present in other list, i.e we are given some of the invalid numbers in one list which needs to be get ridden from the original list and perform its summation. Lets discuss various ways in which this can be performed. 

Method #1 : Using list comprehension + sum() The list comprehension can be used to perform the naive method in just the one line and hence gives an easy method to perform this particular task. The task of performing summation is done using sum(). 

Python3




# Python 3 code to demonstrate
# Summation after elements removal
# using list comprehension + sum()
 
# initializing list
test_list = [1, 3, 4, 6, 7, 6]
 
# initializing remove list
remove_list = [3, 6]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# printing remove list
print ("The remove list is : " + str(remove_list))
 
# using list comprehension + sum() to perform task
res = sum([i for i in test_list if i not in remove_list])
 
# printing result
print ("The list after performing removal summation is : " + str(res))


Output : 

The original list is : [1, 3, 4, 6, 7, 6]
The remove list is : [3, 6]
The list after performing removal summation is : 12

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”.

  Method #2 : Using filter() + lambda + sum() The filter function can be used along with lambda to perform this task and creating a new filtered list of all the elements that are not present in the remove element list. The task of performing summation is done using sum(). 

Python3




# Python 3 code to demonstrate
# Summation after elements removal
# using filter() + lambda + sum()
 
# initializing list
test_list = [1, 3, 4, 6, 7]
 
# initializing remove list
remove_list = [3, 6]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# printing remove list
print ("The remove list is : " + str(remove_list))
 
# using filter() + lambda + sum() to perform task
res = sum(filter(lambda i: i not in remove_list, test_list))
 
# printing result
print ("The list after performing removal summation is : " + str(res))


Output : 

The original list is : [1, 3, 4, 6, 7, 6]
The remove list is : [3, 6]
The list after performing removal summation is : 12

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”.

Method #3 : Using set()
We can use the property of set to remove all the elements of the remove list from the original list.
The set of remove list is created and then the original list is traversed and elements which are not
present in the set are added to the result list. The summation of the final result list is done.

Python3




#Python 3 code to demonstrate
#Summation after elements removal
#using set()
#initializing list
test_list = [1, 3, 4, 6, 7, 6]
 
#initializing remove list
remove_list = [3, 6]
 
#printing original list
print("The original list is : " + str(test_list))
 
#printing remove list
print("The remove list is : " + str(remove_list))
 
#using set() to perform task
res = sum(i for i in test_list if i not in set(remove_list))
 
#printing result
print("The list after performing removal summation is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list is : [1, 3, 4, 6, 7, 6]
The remove list is : [3, 6]
The list after performing removal summation is : 12

Time Complexity : O(n)
Auxiliary Space : O(n)

Method #4: Using a for loop

The given code calculates the summation of the elements in a list after removing specific elements, using a for loop and conditional statements.

Here’s a step-by-step explanation of the algorithm:

  1. Initialize a list of integers test_list.
  2. Initialize a list of integers remove_list to specify which elements to remove from test_list.
  3. Print the original list and the remove list.
  4. Initialize a variable res to 0 to store the sum of the remaining elements.
  5. Loop through each element i in test_list using a for loop.
  6. Check if i is not in remove_list using the not in operator.
  7. If i is not in remove_list, add i to the variable res.
  8. After the loop, return res.

Python3




# Python 3 code to demonstrate
# Summation after elements removal
# using for loop
 
# initializing list
test_list = [1, 3, 4, 6, 7]
 
# initializing remove list
remove_list = [3, 6]
 
# printing original list
print("The original list is : " + str(test_list))
 
# printing remove list
print("The remove list is : " + str(remove_list))
 
# using for loop to perform task
res = 0
for i in test_list:
    if i not in remove_list:
        res += i
 
# printing result
print("The list after performing removal summation is : " + str(res))
#This code is contributed by vinay Pinjala.


Output

The original list is : [1, 3, 4, 6, 7]
The remove list is : [3, 6]
The list after performing removal summation is : 12

The time complexity of this algorithm is O(n), where n is the length of the list test_list. This is because we loop through each element in test_list and check if it is in remove_list, which takes O(1) time on average.

The auxiliary space of this algorithm is O(1), since we only use a fixed amount of additional memory to store the variable res. The memory usage does not depend on the size of the input lists.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments