-2

I have the following dict:

dict = {'1:': ('Mercedes,', '200,', '10000USD'),
        '2:': ('BMW,', '150,', '12000USD'),
        '3:': ('Jeep,', '30,', '8000USD')}

What is the right function to edit values (name, quantity, price)

I want the user to input the ID, and then let him edit the information of the mentioned ID

  • 1
    You can't edit them without rebuilding the entire tuple. You should use lists instead, like `['Mercedes,', '200,', '10000USD']`. – ForceBru Mar 27 '22 at 13:29
  • 2
    `dict[ID] = (name, quantity, price)` ? – Mehrdad Pedramfar Mar 27 '22 at 13:30
  • Do you want to *modify* the value or do you want to *replace* the value? – MisterMiyagi Mar 27 '22 at 13:31
  • @ForceBru No, I can't change the dict, I want to find a way to edit the dict. I can't change it to list –  Mar 27 '22 at 13:33
  • @MisterMiyagi Doesn't matter, just wanted to find a function that makes it possible to edit this dict –  Mar 27 '22 at 13:34
  • Does this answer your question? [How do I change tuple values in a dictionary?](https://stackoverflow.com/questions/54141065/how-do-i-change-tuple-values-in-a-dictionary) – MisterMiyagi Mar 27 '22 at 13:39

1 Answers1

0

When you have given the value in Tuple datatype, you can't edit its elements separately. But, you can entirely replace the existing tuple with new one.

  • Maybe use list datatype instead of tuples. If so, you can use indexing. Like dict[][0] = – vishnuprajit1024 Mar 27 '22 at 13:31
  • 1
    This answer could be significantly improved by providing a short example. This is especially true since the question implies the OP is not familiar with the `dict`/`tuple` API. Note that you can [edit] your answer at any time to expand or adjust it. – MisterMiyagi Mar 27 '22 at 13:34
  • Can you add an example please? –  Mar 27 '22 at 13:40
  • For the question, @VladimirMawla asked, we can use `dict[ID] = (new_model, new_quantity. new_price)` Can't change any element individually inside the tuple since this is an immutable datatype. Instead, use lists like `dict[ID] = [model, quantity, price]`. If so, you can change the values individually. Example: `dict[ID][0] = new_model` – vishnuprajit1024 Mar 27 '22 at 14:26
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – taylor.2317 Mar 27 '22 at 17:49