2D arrays
2D Arrays A 2D array, also known as a two-dimensional array or matrix, is a collection of elements arranged in rows and columns. Each element in the array i...
2D Arrays A 2D array, also known as a two-dimensional array or matrix, is a collection of elements arranged in rows and columns. Each element in the array i...
2D Arrays
A 2D array, also known as a two-dimensional array or matrix, is a collection of elements arranged in rows and columns. Each element in the array is represented by an index, with the row index and column index forming a unique key for that specific element.
Example:
array = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
In this example, the array has 3 rows and 3 columns. Each element in the array is an integer, with the values 1, 2, 3, 4, 5, and 6 representing the elements in the first row, and 7, 8, and 9 representing the elements in the second row.
Key Features:
Rows and Columns: A 2D array consists of rows, which are vertical columns.
Elements: Each element in the array is represented by an index, including the row index and column index.
Multidimensional: A 2D array can contain other 2D arrays as its subarrays, forming a hierarchical structure.
Indexing: Elements in a 2D array can be accessed using both the row index and column index.
Applications:
2D arrays find numerous applications in various programming domains, including:
Representing data: Arrays can be used to store data in a structured format, with elements representing different data types.
Image processing: 2D arrays are commonly used for storing and manipulating images, with each element representing a pixel.
Machine learning: Arrays are used in various machine learning algorithms for data representation and analysis.
Additional Notes:
A 2D array can contain the same element multiple times, as long as each index is unique.
A 2D array can be created dynamically or explicitly using Python lists.
The size of a 2D array is defined by its number of rows and columns, which are independent of the element type