Feb-08-2020, 01:44 AM
(This post was last modified: Feb-08-2020, 01:44 AM by new_to_python.)
Hi, I am new to Python. I came across the following example:
"Like dicts, set elements generally must be immutable. To have list-like elements, you must convert it to a tuple:"
As far as I understand, the elements of a set cannot be changed. Is the goal here to make the elements in the set list-like (changeable) via the tuple function? If so, why my_set[2] still gives an error? I don't quite understand what this example is trying to do nor the quoted statement above the python code. Anybody knows what is happening?
"Like dicts, set elements generally must be immutable. To have list-like elements, you must convert it to a tuple:"
my_data = [1, 2, 3, 4] my_set = {tuple(my_data)}Typing my_set gives: {(1, 2, 3, 4)}
As far as I understand, the elements of a set cannot be changed. Is the goal here to make the elements in the set list-like (changeable) via the tuple function? If so, why my_set[2] still gives an error? I don't quite understand what this example is trying to do nor the quoted statement above the python code. Anybody knows what is happening?