Python for loop, print same items once -


i parse json data url , loop print items want.

import urllib.request import json response = urllib.request.urlopen('http://jsonurl.com') content = response.read() jdata = json.loads(content.decode('utf8')) jdata2 = jdata['available_channels'] values in jdata2.values():     live = values['live']     category = values['category_name']     if "1" in live:       print(category) 

thing if several items have same category prints them multiple times.

for example

drama crime drama drama drama comedy action comedy

i print items have same category once:

drama,crime,comedy,action

how can that?

you can use set keep track of elements have printed. example -

jdata2 = jdata['available_channels'] seen_set = set() values in jdata2.values():     live = values['live']     category = values['category_name']     if "1" in live , category not in seen_set:       print(category)       seen_set.add(category) 

Comments

Popular posts from this blog

xml - Extract substrings with XSLT -

math - "ELO Rating" and how does "TSR Rating" work? -

How can do a tuple comparison in coffeescript similar to that in python? -