Python Forum
Dictionaries in dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionaries in dictionaries
#4
(Dec-12-2018, 09:24 PM)misha Wrote:
for i in data:
    myDict[i[0]] = {i[1]: i[2]}

Instead of setting the dict's value to a dict, set the sub-dict's value, and then make sure there's a sub-dict you can access. Also, use better variable names lol
for key, sub_key, value in data:
    if key not in myDict:
        myDict[key] = {}
    myDict[key][sub_key] = value
Also...
(Dec-12-2018, 09:24 PM)misha Wrote: data = list(map(lambda x: x.split(), fin.readlines()))
You can use str.split directly:
data = list(map(str.split, fin.readlines()))
Reply


Messages In This Thread
Dictionaries in dictionaries - by misha - Dec-12-2018, 08:16 PM
RE: Dictionaries in dictionaries - by ichabod801 - Dec-12-2018, 09:08 PM
RE: Dictionaries in dictionaries - by misha - Dec-12-2018, 09:24 PM
RE: Dictionaries in dictionaries - by nilamo - Dec-12-2018, 09:31 PM
RE: Dictionaries in dictionaries - by misha - Dec-12-2018, 09:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with Dictionaries problem alex_0 2 2,488 Mar-22-2021, 08:25 PM
Last Post: alex_0
  Need insight on dictionaries Mystix 7 6,218 Feb-01-2021, 06:28 PM
Last Post: nilamo
  Help with Python dictionaries kam_uk 5 2,645 Dec-22-2020, 11:32 PM
Last Post: jefsummers
  how can i create a dictionary of dictionaries from a file Astone 2 2,390 Oct-26-2020, 02:40 PM
Last Post: DeaD_EyE
  Iterating over dictionaries with the namedtuple function (PyBite #108) Drone4four 7 5,020 Jul-15-2020, 04:23 AM
Last Post: ndc85430
  QUERY on Looping and creating lists as items within dictionaries ajayachander 3 2,491 Mar-26-2020, 02:03 PM
Last Post: ajayachander
  Need help comparing totals from list of dictionaries AnOddGirl 1 1,647 Mar-18-2020, 01:17 AM
Last Post: AnOddGirl
  Dictionaries in Lists szbeco 6 2,906 Nov-07-2019, 10:16 AM
Last Post: szbeco
  dictionaries and list as values Pippi 6 3,771 Apr-13-2019, 09:05 AM
Last Post: perfringo
  Dictionaries in Python erfanakbari1 1 2,226 Mar-02-2019, 06:40 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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