Bottom Page

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'}
Quote
#2
Loop through the list of dict's, checking if status is on and is registered is yes and add the ones that are to a new list.
Quote
#3
(Jun-02-2019, 06:23 PM)Yoriz Wrote: Loop through the list of dict's, checking if status is on and is registered is yes and add the ones that are to a new list.

I don't need that dictionary if it is on and registered.

If that dictionary say colorkey:red is both on and registered, then I need the dictionary in a second list of dictionaries, that also contains the key vale colorkey:red.


I am working with two lists of dictionaries. The first has the on and registered key values. The second does not.

If any dictionary in the second list is not on OR not registered, then I do not need it.

If any dictionary in the second list is BOTH on and registered, then I do need it.

So I need to loop through the first list of dictionaries to find all that are both on and registered... and then?
Quote
#4
(Jun-02-2019, 06:18 PM)dn237 Wrote: 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.
I have soft deleted a response from this as this sounds like an assignment copied and paste here. Can you explain?
Quote
#5
colors = [
    {'colorkey': 'red', 'status': 'on', 'registered': 'yes'},
    {'colorkey': 'blue', 'status': 'off', 'registered': 'no'},
    {'colorkey': 'green', 'status': 'on', 'registered': 'yes'},
    ]

color_to_find = 'red'
for element in colors:
    # element is a dict
    color = element.get('colorcode')
    if color == color_to_find:
        # result found
        # stop the iteration if you want to have only one result.
        # otherwise you must walk through the complete list
        # if you break out of the lopp
        # you'll only get the first hit
        break
else:
    pass
    # result not found, for loop finished completely
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote
#6
(Jun-02-2019, 10:18 PM)metulburr Wrote:
(Jun-02-2019, 06:18 PM)dn237 Wrote: 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.
I have soft deleted a response from this as this sounds like an assignment copied and paste here. Can you explain?

I don't know what you mean by an assignment copied and pasted here, but if you mean a school assignment that seems to be going way beyond the needed purposes for admins and mods in forums, and to certainly be lowering the quality of the forum just as much as a lack of moderating would as well. I am not a student. If I was, I would have other resources to get help from and so probably wouldn't be here.

What I mean is this:

There is no way to use the index to refer to which dictionary in the list you want. Instead of using the index in the list, the colorkey key value pair has to be used.
Quote
#7
The way things are worded makes it look like homework, lots come here and try to get there homework done for them which does them no good as they don't learn anything.

If its not homework, you have not yet shown the code you need help with.
Are you after someone to do the code for you, if so this can be moved to the jobs section of the forum, otherwise please post a minimal code sample (in python code tags)
for the specific part your stuck on, explain what you expect to happen and what is actually happening and any errors received in error tags.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to find something in a list using its index rix 1 96 Dec-20-2019, 04:12 PM
Last Post: stullis
  item = index position - list of list RavCOder 9 246 Dec-02-2019, 05:24 PM
Last Post: ThomasL
  IndexError: List index out of range issue Adem 1 465 Nov-01-2019, 10:47 PM
Last Post: ichabod801
  IndexError: list index out of range bug? CaptainCsaba 4 754 Oct-18-2019, 12:38 PM
Last Post: CaptainCsaba
  returns index of list if contains a word zarize 0 179 Sep-09-2019, 09:29 AM
Last Post: zarize
  MAWS.py PRGM:IndexError: list index out of range bcramer 0 188 Aug-28-2019, 04:45 PM
Last Post: bcramer
  Select a part of an element of a list with Index BollerwagenIng 0 336 Aug-09-2019, 09:27 AM
Last Post: BollerwagenIng
  Preventing: IndexError: list index out of range PappaBear 1 2,944 Jun-03-2019, 05:50 PM
Last Post: SheeppOSU
  Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV dn237 19 894 May-29-2019, 02:27 AM
Last Post: heiner55
  json.load causing IndexError: list index out of range WallyZ 5 900 May-20-2019, 01:11 PM
Last Post: WallyZ

Forum Jump:


Users browsing this thread: 1 Guest(s)