
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to convert python tuple into a two-dimensional table?
If you have a numeric library like numpy available, you should use the reshape method to reshape the tuple to a multidimensional array.
example
import numpy data = numpy.array(range(1,10)) data.reshape([3,3]) print(data)
Output
This will give the output −
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
Example
If you prefer to do it in pure python, you can use a list comprehension −
data = tuple(range(1, 10)) table = tuple(data[n:n+3] for n in xrange(0,len(data),3)) print(table)
Output
This will give the output −
((1, 2, 3), (4, 5, 6), (7, 8, 9))
- Related Articles
- How to convert JSON data into a Python tuple?
- How to convert a list into a tuple in Python?
- Python program to convert Set into Tuple and Tuple into Set
- How I can convert a Python Tuple into Dictionary?
- How can I convert Python strings into tuple?
- Convert a list into tuple of lists in Python
- How to convert a tuple into an array in C#?
- How can I convert a Python tuple to string?
- How to convert a table into matrix in R?
- How can I append a tuple into another tuple in Python?
- How can I convert a Python tuple to an Array?
- Convert two lists into a dictionary in Python
- How can I convert a Python Named tuple to a dictionary?
- Split one-dimensional array into two-dimensional array JavaScript
- How to convert JSON string into Lua table?

Advertisements