1

I tried to parse json and use for loop in Python. obj is json and as you know it is like dictionary in list in dictionary in dictionary...and so on.

So I parse json and make it as a for loop like this:

all = obj['products']
for a in obj:
      recommendSellerScore.append(a['recommendSellerScore'])

However, the problem is 'recommendSellerScore' which is key does not exist in some lists.

What I want as a result is to print 'None' where recommendSellerScore does not exist, and print values where recommendSellerScore does exist.

for a in all:
    if a['recommendSellerScore'] in all:
        recommendSellerScore.append(a['recommendSellerScore'])
    else:
        continue
        print('None')

I ran this code above, but got an Error because 'recommendSellerScore' does not exist.

for a in all:
    if ['recommendSellerScore'] in all:
        recommendSellerScore.append(a['recommendSellerScore'])
    else:
        continue
        print('None')

This one worked, however, since 'recommendSellerScore' exists in list in dictionary, it was not iterable. (I guess)

Please fix my code, and any comments or advice will be appreciated!

martineau
  • 119,623
  • 25
  • 170
  • 301
J Reigh
  • 47
  • 1
  • 6
  • @hitter thanks for your answer. May I know how I can upvote on comments? Only I can see is a flag on comments. – J Reigh Mar 07 '20 at 17:00

3 Answers3

2

Here is fixed version of your code (I took out the continue statement and the [] around the string):

for a in all:
    if "recommendSellerScore" in a:
        recommendSellerScore.append(a["recommendSellerScore"])
    else:
        print("None")
solid.py
  • 2,782
  • 5
  • 23
  • 30
Ayush Garg
  • 2,234
  • 2
  • 12
  • 28
  • 1
    Don't use `"something" in a` because it might not work as intended if "something" is found in the values. `"something" in a.keys()` is much safer and explicit. – Guimoute Mar 07 '20 at 16:53
  • 1
    @Guimoute you are wrong,`"something" in a` is the same with `"something" in a.keys()` – kederrac Mar 07 '20 at 16:58
  • 1
    this is the best answer so far ;) – kederrac Mar 07 '20 at 17:04
  • @kederrac My bad, I mixed up with `dictview`s. Still a good idea to check explicitly for keys or values. – Guimoute Mar 07 '20 at 17:07
  • 1
    @Guimoute Thank you for your advice! It worked with ".keys()". The code that I use as a result is: for a in all: if "recommendSellerScore" in a.keys(): recommendSellerScore.append(a["recommendSellerScore"]) else: recommendSellerScore.append("None") – J Reigh Mar 07 '20 at 17:10
  • @kederrac I don't know why, I may be missing something in the explanation, however it only works with "something" in a.keys(). Anyway thank you for your comment also! :) – J Reigh Mar 07 '20 at 17:15
2

Maybe something like the code below works for you?

for a in all:
    if a.get(['recommendSellerScore']) is not None:
        recommendSellerScore.append(a['recommendSellerScore'])
  else:
      print("None")
      # No need for continue here.
solid.py
  • 2,782
  • 5
  • 23
  • 30
2

You can use get method of dictionaries to try to access the dictionary value, if the key is not present you can provide a default value, if you don't specify one, it will output None, for example:

key = 'recommendSellerScore'
a = [{key: 1}, {}]

recommendSellerScore = []
for d in a:
    score = d.get(key)
    if score is not None:
        print(score)
        recommendSellerScore.append(score)
    else:
        print('None')

Edit: as pointed by @kederrac if your data can have {key: None} values and you are interested in keeping those None values then it's best to ask if the key is truly present in the dictionary, for example:

key = 'recommendSellerScore'
a = [{key: 1}, {key: None}]

recommendSellerScore = []
for d in a:
    if key in d.keys():
        print(d[key])
        recommendSellerScore.append(d[key])
    else:
        print('None')

It will print:

>>> 1
>>> None 
solid.py
  • 2,782
  • 5
  • 23
  • 30
marcos
  • 4,473
  • 1
  • 10
  • 24