w3resource

Python: Convert string element to integer inside a given tuple using lambda

Python Lambda: Exercise-45 with Solution

Write a Python program to convert string element to integer inside a given tuple using lambda.

Sample Solution:

Python Code :

def tuple_int_str(tuple_str):
    result = tuple(map(lambda x: (int(x[0]), int(x[2])), tuple_str))
    return result     
tuple_str =  (('233', 'ABCD', '33'), ('1416', 'EFGH', '55'), ('2345', 'WERT', '34'))
print("Original tuple values:")
print(tuple_str)
print("\nNew tuple values:")
print(tuple_int_str(tuple_str))

Sample Output:

Original tuple values:
(('233', 'ABCD', '33'), ('1416', 'EFGH', '55'), ('2345', 'WERT', '34'))

New tuple values:
((233, 33), (1416, 55), (2345, 34))

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to calculate the average value of the numbers in a given tuple of tuples using lambda.

Next: Write a Python program to find index position and value of the maximum and minimum values in a given list of numbers using lambda.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

Python: Tips of the Day

Memoization using LRU cache:

import functools

@functools.lru_cache(maxsize=128)
def fibonacci(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    return fibonacci(n - 1) + fibonacci(n - 2)