How to loop through a list of dict and print values without duplicate?

Question:

I’m trying to loop over this data:

data = [{
      "1331":{
         "2022-08-07 to 2022-08-13":10,
         "2022-08-14 to 2022-08-20":8
      },
      "1361":{
         "2022-08-14 to 2022-08-20":4,
         "2022-08-21 to 2022-08-27":4,
         "2022-08-28 to 2022-09-03":5,
         "2022-09-04 to 2022-09-10":4
      },
      "2479":{
         "2022-08-07 to 2022-08-13":18,
         "2022-08-14 to 2022-08-20":28,
         "2022-08-21 to 2022-08-27":34,
         "2022-08-28 to 2022-09-03":27,
         "2022-09-04 to 2022-09-10":20
      },
      "OOO":{
         "2022-08-28 to 2022-09-03":8
      }
   }]
# "AD" being the project name
# "2022-09-04 to 2022-09-10" is the weekly range
# 8 is the hours used for the project

and this should be the output:

1331
10
8
0
0
0
1361
0
4
4
5
4
2479
18
28
34
27
20
OOO
0
0
0
8
0

I’d like to print the values of each project’s hours. If the weekly range isnt found, then print 0.

Here’s my code and output so far:

date = ['2022-08-07 to 2022-08-13', '2022-08-14 to 2022-08-20', '2022-08-21 to 2022-08-27', '2022-08-28 to 2022-09-03', '2022-09-04 to 2022-09-10']

for record in data:
  for project_key, project_value in record.items():
    print(project_key)
    if len(date) == len(project_value.keys()):
      for value in project_value.values():
        print(value)
    else:
      for d in date:
        if d not in project_value.keys():
          print(0)
        else:
          for value in project_value.values():
            print(value)

Output = 
1331
0 # should be 10
0 # should be 8
0
10 # should be 0
8 # should be 0
1361
0
4
4
5
4
2479
18
28
34
27
20
OOO
0
0
0
0 # should be 8
8 # should be 0

When I loop through the list, looks like 0 is being printed out first then the hours. However, I want to print it in order, so if a weekly range isn’t found, print 0 but if it is found, print the hours. How could I implement that? Thank you!

Asked By: MiniDracaena

||

Answers:

for record in data:
    for project_key, project_value in record.items():
        print(project_key)
        for d in date:
            if d in project_value.keys():
                print(project_value[d])
            else:
                print(0)
1331
10
8
0
0
0
1361
0
4
4
5
4
2479
18
28
34
27
20
OOO
0
0
0
8
0
Answered By: Алексей Р

I saw that the dictionary was inside a set, so i removed the unnecessary list bracket and then i also changed code, hope this helps

data = {
  "1331":{
     "2022-08-07 to 2022-08-13":10,
     "2022-08-14 to 2022-08-20":8
  },
  "1361":{
     "2022-08-14 to 2022-08-20":4,
     "2022-08-21 to 2022-08-27":4,
     "2022-08-28 to 2022-09-03":5,
     "2022-09-04 to 2022-09-10":4
  },
  "2479":{
     "2022-08-07 to 2022-08-13":18,
     "2022-08-14 to 2022-08-20":28,
     "2022-08-21 to 2022-08-27":34,
     "2022-08-28 to 2022-09-03":27,
     "2022-09-04 to 2022-09-10":20
  },
  "000":{
     "2022-08-28 to 2022-09-03":8
  }}
date = ['2022-08-07 to 2022-08-13', '2022-08-14 to 2022-08-20', '2022-08-21 to 2022-08-27', '2022-08-28 to 2022-09-03', '2022-09-04 to 2022-09-10']
for record in data:
    print(record)
    for test in date:
         try:
            print(data[record][test])
         except:
            print(0)
Answered By: Jeson Pun

Just to add to the accepted answer, this might be used to get all the dates in your data structure:

date = set()
for d in data[0].values():
    for k in d.keys():
        date.add(k)
date = sorted(list(date))
Answered By: Gábor Fekete
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.