10 examples of 'python iterate over dict' in Python

Every line of 'python iterate over dict' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
885def _iterate_object(obj):
886 """Return iterator over object attributes. Respect objects with
887 defined __slots__.
888
889 """
890 d = {}
891 try:
892 d = vars(obj)
893 except TypeError:
894 # maybe we have named tuple here?
895 if hasattr(obj, '_asdict'):
896 d = obj._asdict()
897 for item in iteritems(d):
898 yield item
899 try:
900 slots = obj.__slots__
901 except AttributeError:
902 pass
903 else:
904 for key in slots:
905 if key != '__dict__':
906 yield (key, getattr(obj, key))
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

464def dict_iter(dct):
465 for k,v in dct.items():
466 if isinstance(v, dict):
467 for kk,vv in dict_iter(v):
468 yield (kk, vv)
469 else:
470 yield (k, v)
206def __iter__(self):
207 return iterdicts(self.table, *self.sliceargs, **self.kwargs)
9def iter_object(o):
10 if hasattr(o, '__slots__'):
11 ignore_attributes = getattr(o, '__sizeof_ignore_attributes__', _DEFAULT_IGNORE_ATTRIBUTES)
12 return (
13 s
14 for s in (getattr(o, slot, _UNSET)
15 for slot in o.__slots__
16 if slot not in ignore_attributes)
17 if s != _UNSET
18 )
19 elif hasattr(o, '__dict__'):
20 ignore_attributes = getattr(o, '__sizeof_ignore_attributes__', _DEFAULT_IGNORE_ATTRIBUTES)
21 return (
22 (k, v) for k, v in list(o.__dict__.items())
23 if k not in ignore_attributes
24 )
25 elif isinstance(o, dict):
26 # Make a copy to avoid corruption
27 return chain.from_iterable(list(o.items()))
28 elif isinstance(o, (list, set, frozenset, tuple, collections.deque)):
29 # Make a copy to avoid corruption
30 return iter(list(o))
31 return []
56def iteritems(coll):
57 return coll.iteritems() if hasattr(coll, 'iteritems') else coll
1126def _IterateDict(self, d, key=""):
1127 if isinstance(d, (list, tuple)):
1128 for i, v in enumerate(d):
1129 next_key = "%s[%d]" % (key, i)
1130 for v in self._IterateDict(v, key=next_key):
1131 yield v
1132 elif isinstance(d, set):
1133 for i, v in enumerate(sorted(d)):
1134 next_key = "%s[%d]" % (key, i)
1135 for v in self._IterateDict(v, key=next_key):
1136 yield v
1137 elif isinstance(d, (dict, rdf_protodict.Dict)):
1138 for k in sorted(d):
1139 k = utils.SmartStr(k)
1140
1141 v = d[k]
1142 if not key:
1143 next_key = k
1144 else:
1145 next_key = key + "." + k
1146
1147 for v in self._IterateDict(v, key=next_key):
1148 yield v
1149 else:
1150 yield key, d
14def iteritems(d):
15 return d.iteritems()
203def iteritems(obj, **kwargs):
204 """replacement for six's iteritems for Python2/3 compat
205 uses 'iteritems' if available and otherwise uses 'items'.
206
207 Passes kwargs to method.
208 """
209 func = getattr(obj, "iteritems", None)
210 if not func:
211 func = obj.items
212 return func(**kwargs)
11def items(dict):
12 return dict.iteritems()
40def __iter__(self):
41 return self.keys()

Related snippets