Python Forum
Code understanding: Need help in understanding dictionary code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code understanding: Need help in understanding dictionary code
#1
Below code is working fine to sum the price for each items. But I could not understand how values-Keys are filled in ordered dict - d.

from collections import OrderedDict
n=int(input())
d=OrderedDict()
for i in range(n):
    item,space,price=input().rpartition(' ')
    price=int(price)
    print("test",d.keys(),d.values())
    if item in d.keys():
        d[item]+=price
    else:
        d[item]=price
for i in d.keys():
    print(i ,d[i])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I am getting an IndentError on my python code in VS Code and i dont know why jcardenas1980 11 3,563 Mar-22-2025, 09:49 AM
Last Post: Pedroski55
  Understanding Key errors JMsc2player 1 804 Nov-03-2024, 09:52 AM
Last Post: snippsat
  Understanding What to test strikeouts27 0 424 Sep-09-2024, 04:25 AM
Last Post: strikeouts27
Shocked Difficulty understanding .moveaxis in Numpy leea2024 2 898 Aug-10-2024, 06:07 AM
Last Post: leea2024
  Merge Python code with Micro Python code? adzy 2 914 Jul-03-2024, 11:41 AM
Last Post: kkinder
  updating pip, python - understanding terminal and pycharm. endlessnameless 0 1,102 Jun-24-2024, 05:48 AM
Last Post: endlessnameless
  Understanding subprocess.Popen Pedroski55 6 1,922 May-12-2024, 10:46 AM
Last Post: Pedroski55
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 4,104 Nov-15-2023, 06:56 PM
Last Post: jst
Question Common understanding of output processing with conditional statement neail 6 2,017 Sep-17-2023, 03:58 PM
Last Post: neail
  Understanding a function ebolisa 3 1,750 Jul-14-2023, 06:03 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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