Trying to count unique values pulled from api requests

Hi! I am a super rookie who is trying to learn through application. I apologize in advance for my incorrect use of terminology or lack thereof.

I am attempting to pull an inventory list from swagger and count unique values. But, after some reading and many failed attempts to achieve the goal, I thought I should start to reach out to the python community for help.

To summarize my struggles so far:

I’m not sure why I am getting a response that breaks down the number of unique, single-digit int.

Any help is appreciated! Thank you.

Read the collections.Counter doc or interactively enter help(Counter). Its input must be an iterable of hashable items. Strings are an iterable of characters. This is illustrated in the docs. You want to give Counter an iterable of sku strings. Try

Counter(title['sku'] for title in data)

to count skus.

1 Like

Thank you for directing me to the collections.Counter doc, Terry! I’m still figuring out best practices for locating resources. I see what you mean now.

I tried Counter(title['sku'] for title in data) but it appears to repeat the counter over and over again. How do I get a single response?

Just kidding.

data = r['Entity']['Tags']
for title in data:
    x = Counter(title['Sku'] for title in data)  
d = dict(x)
print (d)

Resolved my issue. Thanks again.

I’m unconvinced. You compute d = dict(x) for only the last Counter
in the data because that happens after the for-loop completes. That
means it ignores all but the last Sku. I’m not sure this is what you
wanted.

It would help to see the source data (not a screenshot please, just
paste it in as text between triple backticks, just like your code).

Cheers,
Cameron Simpson cs@cskk.id.au