How to create a menu to append, display, and delete items for dictionaries in list?

Hello friends! My list contain over 30 different dictionaries. I want to be able to create a menu which allows the user to append, display, or delete dictionaries from the list. Anyone knows how i can start on it?

You could write a function that prints the list of options:

enter 1 for insert
2 for display

and so forth. Then prompt the user with input() and trigger the function you wan to run

hello. how can i append a new item into my list of dictionaries. for example, if i have this a list inventory_database with items such as
{‘item_description’: ‘chicken wing’, ‘item_selling_price’: ‘$30’, ‘item_stock_level’: 6, ‘item_profit’: 40,‘item_popularity’: 4 / 10}
how do i allow the user to append an item into my list in the same form as my dictionary, prompting the users to fill in the item_description, item_selling_price… etc

you would have to prompt the user for each value, so you would create a dictionary, prompt the user for an item description, then add the item description key and value (provided by user) to dictionary

then after having prompt for all values in the dictionary, append the dictionary to the list

I see, may i ask how do i add the item description key and value from the user input to a dictionary? after i prompt the user for each value, how will i append it systematically into an empty dictionary?

If you don’t know how to add key value pairs to a dictionary is something you can google

Right my bad. Thank you for the help!

Part of programming is figuring stuff out. Learning to be able to search for the right information and learning to read documentation (and other peoples code) is a valuable skill

sure, certain things are difficult to google. Like the flow of your program.

So, my suggestion to google in this case, is not because I don’t want to help you, but because I think using google is better here

1 Like

lists and dicts support various basic operations. for example, you can create an empty list, and you can create an empty dict

a dict supports associating a key with a value, testing whether a key has an associated value, removing a key, finding a value for a key

list supports appending values and removing at the end, as well as reading and writing to any of the existing locations in the list

they both support iteration, allowing traversal, and they’re both aware of their sizes

once you’ve obtained an idea of what they can do, you can start using several operations in succession to describe what you want to do. this is a bit like figuring out how to get a glass of water… you’d open your cabinet where you keep empty glasses, close the door, bring the glass to the tap, turn on water place your glass under the tap … figuring out the individual basic operations shouldn’t be much of a problem

1 Like

@stetim94 @ionatan Thank you so much for the advice. I did manage to append the input values to my list of dictionaries. But i could not find any solution on google to help delete a whole dictionary from a list. rather than only deleting keys from the dictionary, is it possible for a whole dictionary in the list to be deleted after receiving an input from the user when the user only input a value for one key?

You would need to break it down into steps:

can we remove elements from a list? If so, how?

And then take the next step from there

you wouldn’t delete specifically a dictionary, you would look at the operations a list has and carry it out using those. your list doesn’t care in the slightest whether you put dicts or numbers or lists or whatever else in it. it has specific operations and those apply regardless of what you’ve put in your list

I mentioned that list supports removing things at the end. if you mean to remove something elsewhere, you’d have to start moving things around since the only place you can remove at is the end. there are also operations that offer to remove in other locations, but that’s not doing anything different, it’s just doing many things as part of one request.

you’d either remove it or not. there’s no half, or if you mean half, then you actually mean - don’t remove it at all, instead modify the dict. the list would remain completely unmodified.

@ionatan @stetim94 Thank you so much i actually managed to get it running properly now. Having the thought process explained to me really helps more than just staring at the codes on google.

And the trick is to become self-sufficient in the thought process.