Python Forum
How can I change value of dict in list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I change value of dict in list?
#1
I have it
phone_books = [
    {'id': 0, 'name': 'Mike', 'number': 1111111},
    {'id': 1, 'name': 'Sasha', 'number': 2222},
]
I resolved it like that:
for x in phone_books:
    if x.get('id', 0) == 1:
        x['name'] = 'Vika'
        print(x)
Maybe there is better the way ?
Reply


Messages In This Thread
How can I change value of dict in list? - by Mike Ru - May-13-2019, 08:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Change font in a list or tuple apffal 4 2,700 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  How to change the datatype of list elements? mHosseinDS86 9 2,011 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  Membership test for an element in a list that is a dict value for a particular key? Mark17 2 1,228 Jul-01-2022, 10:52 PM
Last Post: Pedroski55
  find some word in text list file and a bit change to them RolanRoll 3 1,549 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  Are list/dict comprehensions interpreted really sequentially? anata2047 3 1,467 May-31-2022, 08:43 PM
Last Post: Gribouillis
  Updating nested dict list keys tbaror 2 1,291 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Change a list to integer so I can use IF statement buckssg 3 2,253 Sep-21-2021, 02:58 AM
Last Post: bowlofred
  change csv file into adjency list ainisyarifaah 0 1,510 Sep-21-2021, 02:49 AM
Last Post: ainisyarifaah
  How to change odd to even numbers in the list? plumberpy 8 3,766 Aug-08-2021, 11:07 AM
Last Post: plumberpy
  Question about change hex string to integer sting in the list (python 2.7) lzfneu 1 2,543 May-24-2021, 08:48 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020