Python Forum
Dictionary iteration and creation a new dictionary from duplicates
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary iteration and creation a new dictionary from duplicates
#1
Hello there,
need an explanation and help how to write a script.
I'm fetching data from OpenStack cinder volumes API. The result is a dictionary with rows of dictionaries.
Every row has specific couple objects which values can be duplicated in another row of the main dictionary.
for example:
{id:gf23gf, size:5}
{id:iu38gr, size:5}
{id:gf23gf, size:5}
{id:iu38gr, size:5}
{id:gf23gf, size:5}
{id:gf23gf, size:5}


My goal is to get these values to a new dictionary. For the same "id" values I need to sum "size" values and result must be as one dictionary row:
{gf23gf:20, iu38gr:10}

my script:
api = [c for c in cinder.volumes.list(search_opts={'all_tenants':1})]
new_dict = {}
for i in api:
....api_dict = {} #rows with dictionaries where is all api's data
........for k,v in api_dict_.iteritems():
............api_dict[k] = v
........id = api_dict['id']
........size = api_dict['size']
........new_dict[id] = size
........if id in new_dict.keys():
............new_dict[id] += size

as an output I have new_dict{}, but the summarization goes incorrect.
Thanks in advance.
Reply
#2
This is what your program does:
new_dict[id] = size
new_dict[id] += size
You logic should be similar to that shown below. For each id/size pair you either add the pair to the dictionary or you increase the size sum. You never do both.
try:
   new_dict[id] += size
KeyError:
   new_dict[id] = size
You can use the test if id in new_dict.keys(), but I think that is horribly inefficient.
Reply
#3
That is great and a helpful suggestion.
Your method gives a correct count.
Thanks so much.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Access individual items of a dictionary by clicking a button AdeS 7 160 Mar-13-2026, 04:36 PM
Last Post: woooee
  I'm assuming you're asking about accessing nested dictionary keys and values in Pytho DavidSah 0 580 Dec-18-2025, 01:54 AM
Last Post: DavidSah
Question [SOLVED] Access keys and values from nested dictionary? Winfried 4 914 Nov-17-2025, 11:47 AM
Last Post: Winfried
  python env with dictionary maiya 2 2,792 Mar-22-2025, 02:07 PM
Last Post: EmilyJohnson
  Replace values in Yaml file with value in dictionary PelleH 1 3,817 Feb-11-2025, 09:51 AM
Last Post: alexjordan
  Building a dictionary .update entry from variables Curbie 5 2,548 Sep-03-2024, 07:31 PM
Last Post: Curbie
  Dictionary using path. Bobbee 5 1,883 Aug-22-2024, 08:43 PM
Last Post: Bobbee
  Modifying a dictionary recursively SpongeB0B 2 1,912 May-12-2024, 04:09 PM
Last Post: Gribouillis
  Sort a list of dictionaries by the only dictionary key Calab 2 2,287 Apr-29-2024, 04:38 PM
Last Post: Calab
Question Using Lists as Dictionary Values bfallert 8 3,938 Apr-21-2024, 06:55 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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