Python Forum
Questions about elements in dict
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?
Reply
#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.
Reply
#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.
Reply
#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.
Reply
#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.
Reply
#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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,013 May-17-2022, 11:38 AM
Last Post: Larz60+
  Sorting Elements via parameters pointing to those elements. rpalmer 3 2,550 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  Sort a dict in dict cherry_cherry 4 62,683 Apr-08-2020, 12:25 PM
Last Post: perfringo
  dict elements are sometimes treated as List and sometimes as String phython_mdr 4 2,645 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 34,445 Nov-25-2017, 06:21 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020