find a specified dictionary in a list

O

Odd-R.

I have this list:

[{'i': 'milk', 'oid': 1}, {'i': 'butter', 'oid': 2},{'i':'cake','oid':3}]

All the dictionaries of this list are of the same form, and all the oids
are distinct. If I have an oid and the list, how is the simplest way of
getting the dictionary that holds this oid?


Thanks in advance
 
J

John Machin

Odd-R. said:
I have this list:

[{'i': 'milk', 'oid': 1}, {'i': 'butter', 'oid': 2},{'i':'cake','oid':3}]

All the dictionaries of this list are of the same form, and all the oids
are distinct. If I have an oid and the list, how is the simplest way of
getting the dictionary that holds this oid?

Something like this:

def oidfinder(an_oid, the_list):
for d in the_list:
if d['oid'] == an_oid:
return d
return None
# These are not the oids you are looking for.
 
J

John Machin

John said:
Odd-R. said:
I have this list:

[{'i': 'milk', 'oid': 1}, {'i': 'butter', 'oid': 2},{'i':'cake','oid':3}]

All the dictionaries of this list are of the same form, and all the oids
are distinct. If I have an oid and the list, how is the simplest way of
getting the dictionary that holds this oid?

Something like this:

def oidfinder(an_oid, the_list):
for d in the_list:
if d['oid'] == an_oid: Better indented as:
> if d['oid'] == an_oid:
return d
return None
# These are not the oids you are looking for.
 
O

Odd-R.

Odd-R. said:
I have this list:

[{'i': 'milk', 'oid': 1}, {'i': 'butter', 'oid': 2},{'i':'cake','oid':3}]

All the dictionaries of this list are of the same form, and all the oids
are distinct. If I have an oid and the list, how is the simplest way of
getting the dictionary that holds this oid?

Something like this:

def oidfinder(an_oid, the_list):
for d in the_list:
if d['oid'] == an_oid:
return d
return None
# These are not the oids you are looking for.

Thank you for your help, but I was hoping for an even simpler
solution, as I am suppose to use it in a
<tal:block tal:define="p python: sentence.

Is there a simpler way of doing it? What if I assume that the
oid I'm searching for really exists?

Thanks again
 
C

Christopher Subich

Odd-R. said:
Odd-R. said:
I have this list:

[{'i': 'milk', 'oid': 1}, {'i': 'butter', 'oid': 2},{'i':'cake','oid':3}]

All the dictionaries of this list are of the same form, and all the oids
are distinct. If I have an oid and the list, how is the simplest way of
getting the dictionary that holds this oid?

Something like this:

def oidfinder(an_oid, the_list):
for d in the_list:
if d['oid'] == an_oid:
return d
return None
# These are not the oids you are looking for.

Thank you for your help, but I was hoping for an even simpler
solution, as I am suppose to use it in a
<tal:block tal:define="p python: sentence.

Is there a simpler way of doing it? What if I assume that the
oid I'm searching for really exists?

If you really, really, really don't care about proper error handling,
both of these expressions should work: (warning, untested since I'm at
work)
right_oid = [d for d in dictlist if d['oid']==the_oid][0]
right_oid = (d for d in dictlist if d['oid']==the_oid).next()

The last one more efficient as a generator expression, but requires
Python2.4. Both of these error in Really Bad Ways (range error and
StopIteration exceptions, respectively) if the right dictionary isn't
in the list.
 
S

Steven D'Aprano

Odd-R. said:
I have this list:

[{'i': 'milk', 'oid': 1}, {'i': 'butter', 'oid': 2},{'i':'cake','oid':3}]

All the dictionaries of this list are of the same form, and all the oids
are distinct. If I have an oid and the list, how is the simplest way of
getting the dictionary that holds this oid?

Instead of keeping a list of dictionaries, keep a dict of dicts, indexed
by the oid:

D = {1: {'i': 'milk'}, 2: {'i': 'butter'}, 3: {'i': 'cake'}}

Then you can extract a dictionary with a single call:

thedict = D[oid]

or just grab the item you want directly:

food = D[oid]['i']


No mess, no fuss. Ninety percent of the work is choosing your data
structures correctly.

Something like this:

def oidfinder(an_oid, the_list):
for d in the_list:
if d['oid'] == an_oid:
return d
return None
# These are not the oids you are looking for.

Thank you for your help, but I was hoping for an even simpler
solution, as I am suppose to use it in a
<tal:block tal:define="p python: sentence.

I don't understand what that means. Can you explain please?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,310
Members
47,977
Latest member
MillaDowdy

Latest Threads

Top