Python - Multiply Integer in Mixed List of string and numbers


Python is a flexible programming language known for its effortlessness and adaptability. It gives a wide run of built-in capacities and data types that make it simple to perform different operations. One common assignment in Python programming is increasing integrability in a mixed list of strings and numbers. In this article, we are going investigate three different approaches to attain this objective, together with step-by-step clarifications and case codes. We'll too cover the vital syntax in Python for each approach. 

Multiplying Integers with Mixed Strings

When managing with a mixed list of strings and numbers in Python, multiplying as it were the integer elements whereas clearing out the strings unchanged requires particular dealing with. One approach includes iterating through the list, checking the sort of each component, and multiplying the integers by a desired figure. Another approach employments list comprehension and a conditional expression to make a modern list with the adjusted elements. Also, useful programming techniques can be applied employing a lambda function and the 'map()' function to duplicate the integers. These approaches give adaptable solutions to perform the required multiplication operation on numbers components inside a mixed list. 

Approach 1: Iteration and Type Checking

The primary approach includes iterating over the components within the list and checking on the off chance that each component is a number. In case it is, we multiply it with a running item.

Algorithm

  • Make a function named 'multiply_integers' that takes a list as an argument

  • Initialize an empty list named 'result' to store the modified components

  • Iterate through each component within the input list using a 'for' loop.

  • Check in case the current component is an occurrence of the 'int' lesson utilizing the 'isinstance()' function.

  • If the component is an integer, increase it by 2 and append the result to the 'result' list

  • In case the component isn't an integer, add it because it is to the 'result' list

  • Return the 'result' list.

  • Call the 'multiply_integers' function with a test mixed list as a contention.

  • Print the returned list. 

Example

def multiply_integers_in_list(lst):
    product = 1
    for element in lst:
        if isinstance(element, int):
            product *= element
    return product

my_list = [2, 53, '478', 50, '666', 17, 'xyz']
result = multiply_integers_in_list(my_list)
print(result)

Output

90100

Approach 2:  Utilizing List Comprehension

The second approach utilizes the control of list comprehension in Python. List comprehension gives a brief way to form lists based on existing lists Here are the steps to take after −

Algorithm

  • Make a function called 'multiply_integers' that takes a list as input

  • Use a list comprehension to repeat over each element within the input list.

  • Within the list comprehension, utilize a conditional expression to check on the off chance that a component is a number.

  • On the off chance that the component is an integer, increase it by 2; something else, keep it unchanged.

  • Return the coming about the list.

  • Call the 'multiply_integers' function with the test mixed list.

  • Print the returned list

Example

from functools import reduce
import operator

def multiply_integers_in_list(lst):
    new_list = [element for element in lst if isinstance(element, int)]
    product = reduce(operator.mul, new_list, 1)
    return product

my_list = [25, 93, '74', 54, '6', 7, 'abc']
result = multiply_integers_in_list(my_list)
print(result)

Output

878850

Approach 3:  Utilizing the filter() and lambda Functions

The third approach utilizes the filter() function in combination with a lambda function. The filter() function makes an iterator from components of an iterable for which a given function returns true. Here's how we will utilize this approach:

Algorithm

  • Make a lambda function that takes a component as input and multiplies it by 2 on the off chance that it is a number; something else, returns the component unchanged.

  • Make a function called 'multiply_integers' that takes a list as a contention

  • Use the 'map()' function beside the lambda function to apply the increase operation to each component within the list.

  • Change over the result of 'map()' back to a list utilizing the 'list()' function.

  • Return the coming about the list.

  • Call the 'multiply_integers' function with the test mixed list.

  • Print the returned list.

Example

from functools import reduce
import operator

def multiply_integers_in_list(lst):
    integers = filter(lambda x: isinstance(x, int), lst)
    product = reduce(operator.mul, integers, 1)
    return product

my_list = [5, 2, 'abc', 3, '6', 'pqr']
result = multiply_integers_in_list(my_list)
print(result)

Output

30

Conclusion

In this article, we investigated three distinctive approaches to multiplying integers in a mixed list of strings and numbers utilizing Python. We examined the step-by-step handle and gave an illustration code for each approach. The primary approach included employing a loop to iterate through the list, whereas the second and third approaches utilized list comprehension and the filter() function with a lambda function, separately. By understanding these approaches, you'll successfully fathom comparative issues in your Python projects. 

Updated on: 01-Sep-2023

83 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements