'how do I can move value from nested dictionary to another key?

How I can write a function that pass this test? this function need to remove DATA from reading_list if match, and add to empty read list

my_data = {'read': [], 'reading_list': [{'c': 'H', 'd': 3.5, 'data': 'this book'}]}
DATA = 'this book'



#this is test case...

def test_moves_book_from_reading_list_to_empty_read():
    # Arrange
    my_data = {
        "reading_list": [{
            "title": DATA,
            "writer": WRITER,
            "rating": RATING_1
        }],
        "read": []
    }

    # Act
    updated_data = read_book(my_data, DATA)

    # Assert
    assert len(updated_data["reading_list"]) is 0
    assert len(updated_data["read"]) is 1



Solution 1:[1]

Hope this is useful.

>>> new_list = []
>>> my_data = {'read': [], 'reading_list': [{'c': 'H', 'd': 3.5, 'data': 'this book'}]}
>>> DATA = 'this book'
>>> if my_data.get('reading_list')[0].get('data') == DATA:
...     new_list.append(my_data.get('reading_list')[0].pop('data'))
... 
>>> my_data
{'read': [], 'reading_list': [{'c': 'H', 'd': 3.5}]}
>>> new_list
['this book']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hegde