Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Questions about elements in dict
#1
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?
Quote
#2
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.
Quote
#3
(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.

my_set[2] raises an error because the set has only one element. That element, the tuple, has four elements of its own.

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.
Quote
#4
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.
Quote
#5
(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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote
#6
(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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Sort a dict in dict cherry_cherry 4 103 9 hours ago
Last Post: perfringo
  dict elements are sometimes treated as List and sometimes as String phython_mdr 4 134 Apr-01-2020, 12:47 PM
Last Post: phython_mdr
  Discord bot that asks questions and based on response answers or asks more questions absinthium 1 3,084 Nov-25-2017, 06:21 AM
Last Post: heiner55

Forum Jump:


Users browsing this thread: 1 Guest(s)