Python | Type casting whole List and Matrix
Sometimes, while working with Python List or Matrix, we usually have a problem in which we require to get a generalized function which could perform the type casting of whole container at once. There is always a need of a mechanism code which can transform entirely the allowed change of data type for all elements in container. Let’s discuss a way in which this task can be performed.
Method : Using
map() + list comprehension
This task can be performed using a map(). The transformation of a normal list can be performed using a single map function by just passing the desired data type. But when it comes to transformation of whole matrix, we need the help of list comprehension for the same.
The original list is : [1, 4, 9, 10, 19] The original Matrix is : [[5, 6, 8], [8, 5, 3], [9, 10, 3]] The List after type casting is : ['1', '4', '9', '10', '19'] The Matrix after type casting is : [['5', '6', '8'], ['8', '5', '3'], ['9', '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