# Two Dimensional Array in Python

The difference between list in python and array in python is a built-in data type in python, the data in list don't have to be the same, and the types in array must all be the same.

An example of a list:

``````a=[1,2,3,4,5] # one dimensional list
b=[[1,2,3],[0,1,2]] # two dimensional list
``````

An example of an array:

``````import numpy as np
a=np.array((1,2,3,4,5))# Parameters are tuple
b=np.array([6,7,8,9,0])# Parameters are list
``````

Creation of a two dimensional array

``````c=np.array([[1,2,3],[4,5,6]]) # Parameters two-dimensional array
``````

To output arrays:

``````print(a,b, c.shape())
``````

Related course: Complete Python Programming Course & Exercises

## Arrays must have same data type

Data types were not used in the creation of arrays earlier, but here we can also use data types. The default is int32.

``````a1=np.array([[1,2,3],[4,5,6]],dtype=np.float64)
print a1.dtype,a.dtype #float64 int32
``````

Previously in the creation of the time we are using the np.array() method from the tuple or list conversion to array, feel very laborious, numpy himself provides a lot of methods for us to create an array directly.

``````arr1=np.array(1,10,1) #
arr2=np.linspace(1,10,10)
print arr1,arr1.dtype
print arr2,arr2.dtype
``````

# Array index

You can access array elements (and list elements) like the examples below:

``````arr #5
arr[3:5] #array([3, 4])
arr[:5] #array([0, 1, 2, 3, 4])
arr[:-1]# array([0, 1, 2, 3, 4, 5, 6, 7, 8])
arr[:] #array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
arr[2:4]=100 # array([ 0, 1, 100, 100, 4, 5, 6, 7, 8, 9])
arr[1:-1:2] #array([ 1, 100, 5, 7]) 2 is interval
arr[::-1] #array([ 9, 8, 7, 6, 5, 4, 100, 100, 1, 0])
arr[5:2:-1]# -1 interval means right to left so 5>2 #array([ 5, 4, 100])
``````

Above is how ARRAY's one-dimensional array is accessed, let's look at how the two-dimensional is handled again

``````print c[1:2]# c[1:2].shape-->(1L, 3L)
print c[1:2] # shape -->(3L,)
print c
print c[1:2]
print c
print c[1:4]
print c[1:4]
``````

## List Index

List indexes can be arrays and lists. the returned data does not share memory with the original data. The index can be list and array.

``````x=np.array(10)
index=[1,2,3,4,5]
arr_index=np.array(index)
print x
print x[index] # list index
print x[arr_index] # array index
``````

Outputs:

``````[0 1 2 3 4 5 6 7 8 9]
[1 2 3 4 5]
[1 2 3 4 5]
``````

## Difference between array and list

The example below shows how lists an arrays can behave differently

``````a=np.change(10)
lista=list(a)
print a*2
print lista*2
``````

Outputs:

``````[0 2 4 6 8 10 12 14 16 18]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
``````