10 examples of 'iterate through dictionary python' in Python

Every line of 'iterate through dictionary python' 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
406def iteritems(self):
407 with _IterationGuard(self):
408 for wr, value in self.data.iteritems():
409 key = wr()
410 if key is not None:
411 yield key, value
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

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
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))
211@cpython_api([PyObject], PyObject)
212def PyDict_Items(space, w_obj):
213 """Return a PyListObject containing all the items from the
214 dictionary, as in the dictionary method dict.items()."""
215 return space.call_method(space.w_dict, "items", w_obj)
143def iteritems(self):
144 """ D.iteritems() -> an iterator over the (key, value) items of D """
145 for wr, value in self.data.items():
146 key = wr()
147 if key is not None:
148 yield key, value
1789Gets the number of key/value pairs contained in the System.Collections.Hashtable.
1790
1791Get: Count(self: Hashtable) -> int
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)
14def iteritems(d):
15 return d.iteritems()
116def iteritems(dictionary):
117 '''Get an iterator over dictionary items.
118
119 Attempts to avoid copying the dictionary items.
120
121 Args:
122 dictionary: The dictionary for which an iterator over items is wanted.
123
124 Returns:
125 An iterator over the dictionary items.
126 '''
127 try:
128 return dictionary.iteritems()
129 except AttributeError:
130 pass
131 return iter(dictionary.items())
206def __iter__(self):
207 return iterdicts(self.table, *self.sliceargs, **self.kwargs)

Related snippets