简体   繁体   中英

“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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM