Python Forum
Dictionaries in dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionaries in dictionaries
#1
Hey guys!
I have a question about dictionaries, namely how I can create a nested dictionary from a loop.
The problem is as follows. I have a farm and several clients buying fruit and vegetables from me.

Smith Apple 100
Davis Apple 70
Smith Potato 40
Smith Apple 10
Brown Cucumber 30
Davis Cucumber 90
Miller Potato 50
Brown Potato 30
Brown Potato 50
Davis Apple 30
Miller Cucumber 40


The first column is the name of the client. The second one is the type of product he/she buys. The third one is the number of sold kilograms.

Here, I have to create a nested dictionary using the data above. The output should be like this:
{'Smith': {'Apple': 110, 'Potato': 40}, 'Davis': {'Apple': 100, 'Cucumber': 90}, Brown: {'Cucumber': 30, 'Potato': 80}, Miller: {'Potato': 50, 'Cucumber': 40}}
I've been trying to crack this problem for two days Sad The best I could get so far is to create a dictionary with names as key. I think when I at least know how to create a value in the form of a dictionary I can try further. Wall

Thank you in advance!
Reply
#2
What does your current code look like?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
It looks raw Confused

fin = open('input.txt', 'r', encoding='utf8')
fout = open('output.txt', 'w', encoding='utf8')
data = list(map(lambda x: x.split(), fin.readlines()))
myDict = {}

for i in data:
    myDict[i[0]] = {i[1]: int(i[2])}

print(myDict, file=fout)
I know that my innerdictionary gets rewritten everyt time there is a new value in the loop. So, that's what I get in the end:
{'Smith': {'Apple': 10}, 'Davis': {'Apple': 30}, 'Brown': {'Potato': 50}, 'Miller': {'Cucumber': 40}}
Reply
#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
#5
Thank you Angel
Reply


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