w3resource

Python: Convert a list of integers, tuple of integers in a list of strings

Python map: Exercise-8 with Solution

Write a Python program to convert a given list of integers and a tuple of integers into a list of strings.

Sample Solution:

Python Code :

# Create a list named 'nums_list' and a tuple named 'nums_tuple' with integer elements
nums_list = [1, 2, 3, 4]
nums_tuple = (0, 1, 2, 3)

# Print the original list and tuple
print("Original list and tuple:")
print(nums_list)
print(nums_tuple)

# Use the map function to convert each element in 'nums_list' to a string and create a new list
result_list = list(map(str, nums_list))

# Use the map function to convert each element in 'nums_tuple' to a string and create a new tuple
result_tuple = tuple(map(str, nums_tuple))

# Print a message indicating the operation to be performed
print("\nList of strings:")

# Print the result of the map operation for the list as a list of strings
print(result_list)

# Print a message indicating the operation to be performed
print("\nTuple of strings:")

# Print the result of the map operation for the tuple as a tuple of strings
print(result_tuple)

Sample Output:

Original list and tuple:
[1, 2, 3, 4]
(0, 1, 2, 3)

List of strings:
['1', '2', '3', '4']

Tuple of strings:
('0', '1', '2', '3')

Python Code Editor:

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

Previous: Write a Python program to add two given lists and find the difference between lists. Use map() function.
Next: Write a Python program to create a new list taking specific elements from a tuple and convert a string value to integer.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/map/python-map-exercise-8.php