Python Forum
List of dictionaries: use key-value instead of index?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List of dictionaries: use key-value instead of index?
#1
If you have a list of dictionaries, and need any dictionary but do not know what index that dictionary is located at, then how can you instead use the key or key value pair to find that dictionary?

The dictionary needs to retrieve as it is within the list so that the key values can still be used the same ways. Or at least be changed back like that.

I feel like this is super simple to do but I'm new to Python and absolutely cannot figure this out nor find it anywhere. I've been looking for days.

Example:

[{'colorkey':'red', 'status':'on', 'registered':'yes'},{'colorkey':'blue', 'status':'off', 'registered':'no'},{'colorkey':green', 'status':'on', 'registered':'yes'}]
Assume there is no way to know what index each colorkey is located at, but you need to retrieve or get or return each dictionary that has a status on and registered yes. You need to find dictionaries not by using the index but by the colorkey key or key value pair.

How can this be done?


Edit:

Here is what should result from that example:

{'colorkey':'red', 'status':'on', 'registered':'yes'},{'colorkey':'green','status':'on','registered':'yes'}
Reply


Messages In This Thread
List of dictionaries: use key-value instead of index? - by dn237 - Jun-02-2019, 06:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a list of dictionaries by the only dictionary key Calab 1 452 Oct-27-2023, 03:03 PM
Last Post: buran
  Access list of dictionaries britesc 4 1,027 Jul-26-2023, 05:00 AM
Last Post: Pedroski55
Thumbs Down I hate "List index out of range" Melen 20 3,155 May-14-2023, 06:43 AM
Last Post: deanhystad
  IndexError: list index out of range dolac 4 1,843 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,408 May-03-2022, 01:39 PM
Last Post: Anldra12
  IndexError: list index out of range rf_kartal 6 2,761 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 2,236 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  IndexError: list index out of range Laplace12 1 2,186 Jun-22-2021, 10:47 AM
Last Post: Yoriz
  function that returns a list of dictionaries nostradamus64 2 1,698 May-06-2021, 09:58 PM
Last Post: nostradamus64
  IndexError: list index out of range brunolelli 11 6,346 Mar-25-2021, 11:36 PM
Last Post: brunolelli

Forum Jump:

User Panel Messages

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