``````

In [1]:

from scipy.sparse import csr_matrix, csc_matrix, coo_matrix, lil_matrix

``````
``````

In [2]:

l = [[1, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 3, 0],
[0, 0, 0, 4]]

``````
``````

In [3]:

csr = csr_matrix(l)
csc = csc_matrix(l)
coo = coo_matrix(l)
lil = lil_matrix(l)

``````
``````

In [4]:

print(csr[1, 1])

``````
``````

2

``````
``````

In [5]:

print(csc[1, 1])

``````
``````

2

``````
``````

In [6]:

print(lil[1, 1])

``````
``````

2

``````
``````

In [7]:

# print(coo[1, 1])
# TypeError: 'coo_matrix' object is not subscriptable

``````
``````

In [8]:

lil[0, 0] = 10
lil[0, 1] = 100

``````
``````

In [9]:

print(lil)

``````
``````

(0, 0)	10
(0, 1)	100
(1, 1)	2
(2, 2)	3
(3, 3)	4

``````
``````

In [10]:

print(lil.toarray())

``````
``````

[[ 10 100   0   0]
[  0   2   0   0]
[  0   0   3   0]
[  0   0   0   4]]

``````
``````

In [11]:

lil[1, 1] = 0

``````
``````

In [12]:

print(lil)

``````
``````

(0, 0)	10
(0, 1)	100
(2, 2)	3
(3, 3)	4

``````
``````

In [13]:

print(lil.toarray())

``````
``````

[[ 10 100   0   0]
[  0   0   0   0]
[  0   0   3   0]
[  0   0   0   4]]

``````
``````

In [14]:

csr[0, 0] = 10

``````
``````

In [15]:

# csr[0, 1] = 100
# SparseEfficiencyWarning: Changing the sparsity structure of a csr_matrix is expensive. lil_matrix is more efficient.

``````
``````

In [16]:

csr[1, 1] = 0

``````
``````

In [17]:

print(csr)

``````
``````

(0, 0)	10
(1, 1)	0
(2, 2)	3
(3, 3)	4

``````
``````

In [18]:

print(csr.toarray())

``````
``````

[[10  0  0  0]
[ 0  0  0  0]
[ 0  0  3  0]
[ 0  0  0  4]]

``````