Skip to content
Advertisement

Element is removed from whole dictonary instead of particular key in dynamic dictonary in python

I am creating a dynamic dictionary. I want to remove the element from a particular array but it is removed from the whole dictionary. In below code I want to remove vehicle id 11 from the time slot “01:01:00 to 01:30:00”. This code removes vehicle id 11 from the whole dictionary.

import json
from datetime import timedelta

time_slots = {}

vehicle_ids = [11, 12]


def convert_time_two_digit(time_to_convert):
    index_of_semicolun = str(time_to_convert).find(":")
    if index_of_semicolun == 1:
        return "0" + time_to_convert
    else:
        return time_to_convert


def get_default_time_slot():
    for i in range(0, 5):
        if i == 0:
            start_time = timedelta(minutes=00)
            end_time = start_time + timedelta(minutes=30)
        else:
            start_time = end_time + timedelta(minutes=1)
            end_time = start_time + timedelta(minutes=29)
        # print("startTime : {}, end_time: {}".format(start_time, end_time))
        default_available_vehicle = {"vehicle_ids": vehicle_ids}
        time_slots[convert_time_two_digit(str(start_time)) + " to " + convert_time_two_digit(str(end_time))] = 
            default_available_vehicle


get_default_time_slot()
timeslotsJson = json.dumps(time_slots, indent=4)
print(timeslotsJson)

time_slots["01:01:00 to 01:30:00"]["vehicle_ids"].remove(11)
timeslotsJson = json.dumps(time_slots, indent=4)
print(timeslotsJson)

Actual Output

{
    "00:00:00 to 00:30:00": {
        "vehicle_ids": [
            12
        ]
    },
    "00:31:00 to 01:00:00": {
        "vehicle_ids": [
            12
        ]
    },
    "01:01:00 to 01:30:00": {
        "vehicle_ids": [
            12
        ]
    },
    "01:31:00 to 02:00:00": {
        "vehicle_ids": [
            12
        ]
    },
    "02:01:00 to 02:30:00": {
        "vehicle_ids": [
            12
        ]
    }
}

Expected output should be

{
    "00:00:00 to 00:30:00": {
        "vehicle_ids": [
            11, 12
        ]
    },
    "00:31:00 to 01:00:00": {
        "vehicle_ids": [
            11, 12
        ]
    },
    "01:01:00 to 01:30:00": {
        "vehicle_ids": [
            12
        ]
    },
    "01:31:00 to 02:00:00": {
        "vehicle_ids": [
            11, 12
        ]
    },
    "02:01:00 to 02:30:00": {
        "vehicle_ids": [
            11, 12
        ]
    }
}

It is working fine when I define a static dictionary. But that is not the solution. Thanks

Advertisement

Answer

Your default_available_vehicle is a dictionary that contains a reference to the same list for all timestamps. This means that when you attempt to remove an element from the list e.g. using logic similar to the following:

time_slots["02:01:00 to 02:30:00"]["vehicle_ids"].remove(11)

It will appear as if 11 has been removed for all the timestamps since they are referring to the same global list.

One solution, assuming you wish to keep the global vehicle_ids list, is to create a new copy of the list in your loop. You can do this by using vehicle_ids.copy() instead i.e.

default_available_vehicle = {"vehicle_ids": vehicle_ids.copy()}

Alternatively, you may wish to restructure your code to remove the need for the global variable. You can simply initialise the list in your loop if the values for the list are known:

default_available_vehicle = {"vehicle_ids": [11, 12]}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement