Otherwise nice, but maybe v is not sequence.
Here is other form with del. You must iterate over d.keys() as dictionary changes during iteration:
d={'a':[],'b':['1','2'],'c':[], 'd':False, 'e':'', 'f': 0.0}
for k in d.keys():
try:
if len(d[k])<1:
del d[k]
except: pass
print d
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
Just tell Python to make it dictionary:
d={'a':[('X','1'),('X','2')],'b':[('Y','1')]}
for key, item in d.items():
d[key] = dict(item)
print d
""" Output:
{'a': {'X': '2'}, 'b': {'Y': '1'}}
"""
But because it has double keys, must do tricks:
d={'a':[('X','1'),('X','2')],'b':[('Y','1')]}
for key, item in d.items():
d2 = {}
for key2, item2 in item:
d2[key2] = d2[key2]+ [item2] if key2 in d2 else [item2]
d[key] = d2
print d
""" Output:
{'a': {'X': ['1', '2']}, 'b': {'Y': ['1']}}
"""
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852