Sometimes, while working with data, we can have a problem in which we have data in form of tuple Matrix with uneven length rows. In this case there’s a requirement to complete the N*N matrix with a default value. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using loop +
This problem can be solved using loop. This is brute force method to perform this task. We just append the default value as many times, as the data is missing in a row than N.
The original tuple is : ((5, 4), (3, ), (1, 5, 6, 7), (2, 4, 5)) Tuple after filling values : [(5, 4, 0, 0), (3, 0, 0, 0), (1, 5, 6, 7), (2, 4, 5, 0)]
Method #2 : Using
tuple() + generator expression
Similar task can be performed in one line using generator expression. In this, similar logic is applied as above just zipped as one-liner. The
tuple(), changes result to tuple.
The original tuple is : ((5, 4), (3, ), (1, 5, 6, 7), (2, 4, 5)) Tuple after filling values : ((5, 4, 0, 0), (3, 0, 0, 0), (1, 5, 6, 7), (2, 4, 5, 0))
- Python | Dictionary to list of tuple conversion
- Python | Remove similar element rows in tuple Matrix
- Python | Sort tuple list by Nth element of tuple
- Python - Raise elements of tuple as power to another tuple
- Python | Replace tuple according to Nth tuple element
- Python | Key-Value to URL Parameter Conversion
- Python | Timezone Conversion
- C strings conversion to Python
- Type Conversion in Python
- Python | List of tuples to dictionary conversion
- HTML Cleaning and Entity Conversion | Python
- Python | Type conversion in dictionary values
- Python | List of float to string conversion
- Python | Type conversion of dictionary items
- Python | Decimal to binary list conversion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.