You (Probably) Don’t Need For-Loops
Randy Daw-Ran Liou
41113

Thanks for article. However, how would you rewrite and optimize the following code:

d = {(‘10.0.0.1’,’10.0.0.5',4254,80):1,(‘10.0.0.2’,’10.0.0.5',5366,80):2,(‘10.0.0.3’,’10.0.0.5',6464,80):3, (‘10.0.0.4’,’10.0.0.5',5999,80):4,(‘10.0.0.1’,’10.0.0.5',9135,80):80}
ip_server = {‘10.0.0.1’:3,’10.0.0.2':3,’10.0.0.4':4}

values={}

for i in range(len(ip_server)):
 search_key = (ip_server.keys()[i],)
 values[search_key[0]] = len([value for key, value in d.items() if search_key == key[:1]])