DeepDiff 7.0.1 documentation!


deepdiff.path.extract(obj, path)

Get the item from obj based on path.


>>> from deepdiff import extract
>>> obj = {1: [{'2': 'b'}, 3], 2: [4, 5]}
>>> path = "root[1][0]['2']"
>>> extract(obj, path)

Note that you can use extract in conjunction with DeepDiff results or even with the search and DeepSearch modules. For example:

>>> from deepdiff import grep
>>> obj = {1: [{'2': 'b'}, 3], 2: [4, 5]}
>>> result = obj | grep(5)
>>> result
{'matched_values': OrderedSet(['root[2][1]'])}
>>> result['matched_values'][0]
>>> path = result['matched_values'][0]
>>> extract(obj, path)


Note that even if DeepDiff tried gives you a path to an item in a set, there is no such thing in Python and hence you will get an error trying to extract that item from a set. If you want to be able to get items from sets, use the OrderedSet module to generate the sets. In fact Deepdiff uses OrderedSet as a dependency.

>>> from deepdiff import grep, extract
>>> obj = {"a", "b"}
>>> obj | grep("b")
Set item detected in the path.'set' objects do NOT support indexing. But DeepSearch will still report a path.
{'matched_values': OrderedSet(['root[0]'])}
>>> extract(obj, 'root[0]')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "deepdiff/deepdiff/", line 126, in extract
    return _get_nested_obj(obj, elements)
  File "deepdiff/deepdiff/", line 84, in _get_nested_obj
    obj = obj[elem]
TypeError: 'set' object is not subscriptable
>>> from deepdiff.helper import OrderedSetPlus
>>> obj = OrderedSetPlus(["a", "b"])
>>> extract(obj, 'root[0]')

Back to DeepDiff 7.0.1 documentation!