Questions about elements in dict - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Questions about elements in dict (/thread-24304.html) |
Questions about elements in dict - new_to_python - Feb-08-2020 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:" 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? RE: Questions about elements in dict - stullis - Feb-08-2020 The key here is mutability. Tuples are immutable - they cannot be changed. As such, they are hashable and can be used as elements of a set. Conversely, lists are mutable and cannot be used as set elements. my_set[2] raises an error because the set has only one element. That element, the tuple, has four elements of its own. RE: Questions about elements in dict - new_to_python - Feb-08-2020 (Feb-08-2020, 03:07 AM)stullis Wrote: The key here is mutability. Tuples are immutable - they cannot be changed. As such, they are hashable and can be used as elements of a set. Conversely, lists are mutable and cannot be used as set elements. Thanks. In this case, how do I access individual element(s) in the tuple which is the only element in the dict? As far as I know, there is no ordering among the elements in a set. RE: Questions about elements in dict - ndc85430 - Feb-08-2020 Do you really want the tuple in the set, or do you actually want a set of the individual values? In any case, you're right, the idea of ordering doesn't make sense for a set (or dict). Ordering has been sacrificed for speed - checking whether an item is in a set can be done in constant time, as opposed to linear time for an unsorted list or tuple. RE: Questions about elements in dict - perfringo - Feb-08-2020 (Feb-08-2020, 05:25 AM)ndc85430 Wrote: the idea of ordering doesn't make sense for a set (or dict). dict part is not correct 3.6 <= Python. In 3.6 they are insertion ordered as implementation detail (What's new 3.6 >>> New dict implementation) but from 3.7 the insertion ordering is guaranteed (What's new 3.7 >>> Summary Release Highlights) Quote:the insertion-order preservation nature of dict objects has been declared to be an official part of the Python language spec. RE: Questions about elements in dict - new_to_python - Feb-08-2020 (Feb-08-2020, 05:25 AM)ndc85430 Wrote: Do you really want the tuple in the set, or do you actually want a set of the individual values? I am just trying to understand some concepts/examples presented in books. I guess the goal is to make the elements in the set list-like (changeable) via the tuple function. It is not clear to me how the example accomplishes that. |