473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

find a specified dictionary in a list

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

--
Har du et kjøleskap, har du en TV
så har du alt du trenger for å leve

-Jokke & Valentinerne
Jul 22 '05 #1
5 1251
Odd-R. wrote:
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.

Jul 22 '05 #2
John Machin wrote:
Odd-R. wrote:
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.

Jul 22 '05 #3
On 2005-07-22, John Machin <sj******@lexicon.net> wrote:
Odd-R. wrote:
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
--
Har du et kjøleskap, har du en TV
så har du alt du trenger for å leve

-Jokke & Valentinerne
Jul 22 '05 #4


Odd-R. wrote:
On 2005-07-22, John Machin <sj******@lexicon.net> wrote:
Odd-R. wrote:
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.

Jul 22 '05 #5
On Fri, 22 Jul 2005 12:42:04 +0000, Odd-R. wrote:
On 2005-07-22, John Machin <sj******@lexicon.net> wrote:
Odd-R. wrote:
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?
--
Steven.

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John Mudd | last post by:
I must be missing something here. It's clearly faster to lookup an item directly in a dictionary than to scan through a list. So when I have a large lookup table I always load it in the form of a...
3
by: Jeff | last post by:
I am using SQL Server 2000. I have a table with, say, 20 columns. I have one procedure which updates all 20 columns at once, accepting a parameter for each column. However, I want to be able to...
3
by: Radu | last post by:
Hi. I have the following: ___________________________________________________________________________________ Public Class SquareDictionary Inherits System.Collections.DictionaryBase Default...
6
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: ...
0
by: xpding | last post by:
Hello, I have a class MyEmbededList contains a generic dictionary, the value field is actually the MyEmbededList type as well. There is another class need to access and manipulate a list of...
3
by: Alexander Vasilevsky | last post by:
Here, there is a challenge: to find the files must be on a local computer, have the same names. Get drives and folders to go through without problems. But what and how to store files while a...
11
by: Dan Upton | last post by:
This might be more information than necessary, but it's the best way I can think of to describe the question without being too vague. The task: I have a list of processes (well, strings to...
0
by: Gary Herron | last post by:
Dan Upton wrote: Yes. Create a list of keys, and loop through it: pids = procs_dict.keys() for pid in pids: if procs_dict.poll() != None # do the counter updates del procs_dict Then the...
14
by: lee | last post by:
hi, i have a dictionary as follows : kev : {'phno': , 'email': , 'name': , 'address': } if user is enters the 3rd item of key phno, ie "dfsdf" in my dict, how can i find it is the third item...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.