Python Forum
Working with dictionaries and keys
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Working with dictionaries and keys
#10
More of that. The order of the dict in Python 3.6 fallow the order of inserting its elements.

So, if you delete a key and it's value and insert again the same key with a new value they will be positioned after all the others when printing it.
In [1]: dict_ = {num: num**2 for num in range(2,5)}

In [2]: dict_
Out[2]: {2: 4, 3: 9, 4: 16}

In [3]: dict_.pop(3)
Out[3]: 9

In [4]: dict_
Out[4]: {2: 4, 4: 16}

In [5]: dict_[3] = 3**3

In [6]: dict_                 # here the order is preserved because of the interpreter. It's doing it
Out[6]: {2: 4, 3: 27, 4: 16}

In [7]: dict_[1] = 'one'

In [8]: dict_
Out[8]: {1: 'one', 2: 4, 3: 27, 4: 16} # the same  

In [9]: print(dict_)                          # but
{2: 4, 4: 16, 3: 27, 1: 'one'}
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Messages In This Thread
Working with dictionaries and keys - by netrate - Jun-01-2017, 12:28 AM
RE: Working with dictionaries and keys - by netrate - Jun-01-2017, 01:20 AM
RE: Working with dictionaries and keys - by netrate - Jun-01-2017, 02:22 AM
RE: Working with dictionaries and keys - by buran - Jun-01-2017, 02:41 AM
RE: Working with dictionaries and keys - by buran - Jun-01-2017, 02:45 AM
RE: Working with dictionaries and keys - by Larz60+ - Jun-01-2017, 04:46 AM
RE: Working with dictionaries and keys - by wavic - Jun-01-2017, 05:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Question about working with dictionaries Ashcora 13 2,118 Dec-27-2022, 09:09 PM
Last Post: Ashcora
  Best way to support multiple keys in dictionaries mrapple2020 3 2,688 Apr-06-2019, 06:54 AM
Last Post: mrapple2020
  Working with files and dictionaries OmarSinno 1 2,624 Oct-30-2017, 11:02 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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