0

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?

4
  • 2
    It's a list of tuples and tuples are immutable. You have to create a new list Commented Aug 11, 2020 at 7:05
  • Thanks for the reply, but can you please tell me how to copy the values from the table to a new list? Because I've tried different methods but the outcomes were still the same Commented Aug 11, 2020 at 7:12
  • Can you post sample data stored in the variable table and also the expected output? Commented Aug 11, 2020 at 7:13
  • Nevermind, I figured out, thank you so much for the answer, I replaced the code table = list(c.fetchall()) by for i in c.fetchall(): table.append(list(i)) Commented Aug 11, 2020 at 7:21

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.