Home > Mobile >  how to seach, get values and update data in list of lists python
how to seach, get values and update data in list of lists python

Time:03-10

list_A = [['STEVEN', 200], ['SUSAN', 100], ['NICRO', 115], ['MIKE', 320], ['JOHN', 50], ['GIGEE', 270]]

I want to search for name: search_name = "NICRO"

and program will give me: pocket_money = 115

after that I do need to update the values

addition_money = 9000

update_money = pocket_money addition_money

then put it back to the list

list_A = [['STEVEN', 200], ['SUSAN', 100], ['NICRO', 9115], ['MIKE', 320], ['JOHN', 50], ['GIGEE', 270]]

How to do this code.

CodePudding user response:

Search for money:

name = input("Enter name: ")
for i in list_A:
    if i[0] == name:
        print(i[1])

Update money:

additional_money = 9000
for i in range(len(list_A)):  #can't use for i in list_A because need index for reference
    if list_A[i][0] == "NICRO":
        list_A[i][1]  = additional_money

        break  #(optional)
  • Related