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
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 1,052 Nov-15-2023, 06:56 PM
Last Post: jst
Question Common understanding of output processing with conditional statement neail 6 891 Sep-17-2023, 03:58 PM
Last Post: neail
  Understanding a function ebolisa 3 796 Jul-14-2023, 06:03 PM
Last Post: snippsat
  Understanding venv; How do I ensure my python script uses the environment every time? Calab 1 2,291 May-10-2023, 02:13 PM
Last Post: Calab
  New to python/coding Need help on Understanding why this code isn't working. Thanks! mat3372 8 1,761 May-09-2023, 08:47 AM
Last Post: buran
  Understanding and debugging memory error crashes with python3.10.10 Arkaik 5 2,114 Apr-18-2023, 03:22 AM
Last Post: Larz60+
  Understanding Python classes PythonNewbee 3 1,194 Nov-10-2022, 11:07 PM
Last Post: deanhystad
  python multiple try except block in my code -- can we shorten code mg24 10 6,177 Nov-10-2022, 12:48 PM
Last Post: DeaD_EyE
  Problems with understanding class index. Brainbit 3 939 Sep-20-2022, 07:52 PM
Last Post: Brainbit
  Need help Understanding JSON quarinteen 3 1,072 Aug-15-2022, 08:33 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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