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']]
- Python | Unpack whole list into variables
- PyQt5 QSpinBox – Getting whole text
- PyQt5 QSpinBox - Setting font to the whole text
- PyQt5 QDateEdit - Selecting Whole Date
- PyQt5 QDoubleSpinBox – Getting Whole Text
- PYGLET – Setting Style to whole Formatted Document
- Python - Type conversion in Nested and Mixed List
- Python - Matrix Data Type Rectification
- Python | Convert heterogenous type String to List
- Python - Split heterogeneous type list
- Python - Test if all elements in list are of same type
- Python - Change type of key in Dictionary list
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert List of String List to String List
- Python program to Convert a Matrix to Sparse Matrix
- Python - Convert Matrix to Custom Tuple Matrix
- type and isinstance in Python
- Python | Float type and its methods
- Python List Comprehension | Segregate 0's and 1's in an array list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.