Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python dictionnary
#1
i just started learning python and following an online course
in the course there is this dictionary :
pairs = {1: "apple", "orange": [2, 3, 4], True: False, None: "True"}
when using print(pairs) i get this output
{1: False, 'orange': [2, 3, 4], None: 'True'}
why is that?
Reply
#2
Dictionaries in python are not "ordered" like lists.
Reply
#3
True and False are actually subclasses of integers, with True equal to 1 and False equal to 0. You can test this by adding them to normal integers. So when Python gets to 'True: False', it sets the value of 1 to False, overriding the previous value of 'apple'.

To be more specific, if hash(x) == hash(y) and x == y, then dictionaries will treat x and y as the same key. That's what is going on here.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Dec-07-2019, 07:54 PM)michael1789 Wrote: Dictionaries in python are not "ordered" like lists.

I've not yet used this feature, but Python dictionaries are ordered by default since Python3.7.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Dictionnary indexing error Ander 6 1,833 Sep-10-2021, 12:22 PM
Last Post: Ander
  Dictionnary brackets issue Reldaing 1 1,784 Nov-10-2019, 11:54 PM
Last Post: ichabod801
  Access to the elements of a dictionnary Reims 1 1,603 Oct-02-2019, 12:48 PM
Last Post: SheeppOSU
  from Json Time Serie file to python dictionnary Reims 1 1,990 Sep-11-2019, 08:17 AM
Last Post: DeaD_EyE
  convert a json file to a python dictionnary of array Reims 2 2,205 Sep-10-2019, 01:08 PM
Last Post: Reims
  dictionnary lateublegende 1 2,417 Apr-29-2019, 09:10 PM
Last Post: Yoriz
  Why do we need setdefault() method for dictionnary? DJ_Qu 3 2,662 Apr-21-2019, 11:00 AM
Last Post: Gribouillis
  Json dictionnary on Redis katsu707 1 2,411 Dec-04-2018, 11:59 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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