I
I
Igor Che2016-04-14 16:00:37
Python
Igor Che, 2016-04-14 16:00:37

Why does Python wrap list elements in tuples?

Python wraps each list element followed by a comma into a tuple.

fio = {},
age = {},
date = {},
diagnoz = {}

columns = [
    fio,
    age,
    date,
    diagnoz,
]
print(columns)

I get this result:
[({},), ({},), ({},), {}]
I'm actually trying to add data to the dictionaries in a loop. But due to the fact that each element of the dictionary is wrapped in a tuple, I cannot do this and get an error:
TypeError: 'tuple' object does not support item assignment

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander, 2016-04-14
@chewarer

No commas are needed in the first three lines.
A newline is already a sufficient instruction separator.
A comma is not used to separate instructions, only to separate elements in different sequences. It should be used inside brackets (either round, or square, or curly). If you use a comma without parentheses, then the interpreter assumes parentheses (i.e., a tuple) are implied.
Those. you need to write like this:
fio = {}
age = {}
date = {}
diagnoz = {}
By the way, the diagnoz variable - are you writing some kind of medical system? I just work in a medical center myself :))

R
Rostislav Grigoriev, 2016-04-14
@crazyzubr

Because the record

x = ('string',)
y = (1,)

Equivalent
x = 'string',
y = 1,

I
Igor Che, 2016-04-14
@chewarer

Thanks to all.
I copied these lines from the list, but did not pay attention to the commas.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question