Home > Software design >  add and delete items in data set using python
add and delete items in data set using python

Time:06-02

I need a develop a taxi booking app. One of the features is adding a new vehicle to the available vehicle list and if you want you can be able to remove it from the vehicle list.

I use a dictionary data structure to implement this problem,

enter image description here

This code will display the available vehicle list

vehicals = {"car":["3-4","AC/Non A/C"],"van":["6-8"],"3 Wheelers":["3","Not Avilable A/C"]}
number_of_items = len(vehicals)
i=1

print("Available Vehicals List")
for key, value in vehicals.items():
    print(str(i) "." key)
    i =1

so I want to update this list after adding a new item to this dictionary or delete an item from this list.

CodePudding user response:

you can add a new item by simply:

vehicles['new_item'] = value

and delete an old one by:

vehicles.pop('old_item')

CodePudding user response:

To add new data it is simple. You have

  1. vehicals['new_car'] = value
  2. For example you already have one following dict

new_data = {'new_car': ["5-12"], 'new_car_2': ["4-6"]}',

you can simply update old dict by vehicals.update(new_data)

to delete old ones you also have two ways:

  1. simply vehicals.pop('car_to_delete')
  2. del vehicals['car_to_delete']

hope it helps

  • Related