qwea
qwea

Reputation: 525

"TypeError: 'tuple' object does not support item assignment" on a list object

I imported a table from a database by using sqlite3, and then converted it to a list, even the type() function returned that the class was a list, but it still gave me that error message when I tried to change one of the cells

Here's my code:

import sqlite3

conn = sqlite3.connect('test.db')
c = conn.cursor()
c.execute("SELECT * FROM table1")
table = list(c.fetchall())
print(type(table))

table[0][0] = '10'

Output:

<class 'list'>
Traceback (most recent call last):
  File "PATH", line 9, in <module>
    table[0][0] = '10'
TypeError: 'tuple' object does not support item assignment

Can anyone please tell me what's wrong?

Upvotes: 0

Views: 2238

Answers (1)

khelwood
khelwood

Reputation: 59112

You can convert your list of tuples to a list of lists using the map function.

table = list(map(list, table))

Then you will be able to reassign table[0][0], assuming such an element exists.

Upvotes: 1

Related Questions