py_coder1019
py_coder1019

Reputation: 115

How to Edit Specific Dictionary Key Values in List of Dictionaries?

I have a list of dictionaries like so, named result

{'Item #': 2, 'Price ': 2.99, 'Quantity': 2, 'Name': 'Muffin2'}
{'Item #': 3, 'Price ': 3.99, 'Quantity': 3, 'Name': 'Cake1'}
{'Item #': 4, 'Price ': 4.99, 'Quantity': 4, 'Name': 'Cake2'}

I am trying to develop methods that upon user input, can edit the key values of a specific dictionary in the list. I want to traverse the dictionary, find the dictionary that has the Item # key value equal to the input, and then allow the user to edit the price, quantity, and name key values of the dictionary that matches their input.

I was wondering what is the best way to do this?

I was able to come with something like this for an idea, but wasn't sure if this was on the right path or idea wise?


 def enter_data(self, message, typ):
            while True:
                try:
                    v = typ(input(message))
                except ValueError:
                    print(f"Thats not an {typ}!")
                    continue
                else:
                    break
            return v

  def edit_item_interaction(self):
            self.database.show_available()
            edit_specific_item = self.enter_data("Enter the item # of the item you would like edit\n" , int)
            for item in self.database.result:
                if item["Item #"] == edit_specific_item:
                    index = #Index of dictionary in list where it was found?
                    break
            break
        float(change_price = self.enter_data("Enter the change in price\n" , float))
        self.databse.result[index]["Price "].append(change_price)
        self.database.result[index]["Price "].pop(1)

How do I keep track of the index where the dictionary is in the list, that matches the key value I am looking for?

For example, If a user inputs they would like to edit item # 2, it should traverse the list of dicts to find the dictionary where the item number is, get the index of where this dictionary is in the list, and the use those two variables to change the Price Value Key for that dictionary, and pop out the old value.

Am I on the right track?

Upvotes: 0

Views: 610

Answers (1)

Chris
Chris

Reputation: 36596

It's rather simpler than this to find the dictionary that matches a specific 'Item #' key value, using next.

>>> result = [
...   {'Item #': 2, 'Price ': 2.99, 'Quantity': 2, 'Name': 'Muffin2'},
...   {'Item #': 3, 'Price ': 3.99, 'Quantity': 3, 'Name': 'Cake1'},
...   {'Item #': 4, 'Price ': 4.99, 'Quantity': 4, 'Name': 'Cake2'}
... ]
>>> next((d for d in result if d['Item #'] == 3), None)
{'Item #': 3, 'Price ': 3.99, 'Quantity': 3, 'Name': 'Cake1'}

Having found that, you simply need to modify it as you see fit. For demonstration's sake, changing the price to 67.

>>> d = next((d for d in result if d['Item #'] == 3), None)
>>> d['Price '] = 67
>>> result
[{'Item #': 2, 'Price ': 2.99, 'Quantity': 2, 'Name': 'Muffin2'}, {'Item #': 3, 'Price ': 67, 'Quantity': 3, 'Name': 'Cake1'}, {'Item #': 4, 'Price ': 4.99, 'Quantity': 4, 'Name': 'Cake2'}]

Upvotes: 1

Related Questions