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 | Tuple key dictionary conversion
- Python | Dictionary to list of tuple conversion
- Python | Matrix Tuple pair Column product
- Python | Remove similar element rows in tuple Matrix
- Python - Raise elements of tuple as power to another tuple
- Python - Convert Tuple String to Integer Tuple
- Python | Sort tuple list by Nth element of tuple
- Python | Replace tuple according to Nth tuple element
- C strings conversion to Python
- Type Conversion in Python
- Python | Timezone Conversion
- Python | Key-Value to URL Parameter Conversion
- Python | Empty String to None Conversion
- Python: Weight Conversion GUI using Tkinter
- Python | List of tuples to dictionary 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.