473,326 Members | 2,148 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,326 software developers and data experts.

efficient matching of elements a list

Suppose I have a lists of tuples
A_LIST=[(1,6,7),(7,4,2),(7,9,2),(1,5,5),(1,1,1)]
and an int
i=1
What is the fastest way in python to get out all the tuples from the
list whose first element is equal to i?
A have a very large list and a simple
for a in A_LIST:
if a[0]==i:
#We have a match!!

Seems to be very slow and there must be some super quick pythonic way to
do this maybe?
Any avice would be much appreciated!!

Jul 18 '05 #1
4 1644
omission9 <om*******@invalid.email.info> writes:
Suppose I have a lists of tuples
A_LIST=[(1,6,7),(7,4,2),(7,9,2),(1,5,5),(1,1,1)]
and an int
i=1
What is the fastest way in python to get out all the tuples from the
list whose first element is equal to i?
t = [x for x in A_LIST if x[0] == i]
Seems to be very slow and there must be some super quick pythonic way
to do this maybe?
Any avice would be much appreciated!!


Maybe there's some obscure faster way to do it but if you're really
in a hurry, use psyco or pyrex or write a C extension.
Jul 18 '05 #2
>>Suppose I have a lists of tuples
A_LIST=[(1,6,7),(7,4,2),(7,9,2),(1,5,5),(1,1,1)]
and an int
i=1
What is the fastest way in python to get out all the tuples from the
list whose first element is equal to i?

t = [x for x in A_LIST if x[0] == i]


Before list comprehensions, we had to do it all with filter...
t = filter(lambda x:x[0]==i, A_LIST)

List comprehension seem to be around 33% faster. Long live list
comprehensions.

- Josiah
Jul 18 '05 #3
> What is the fastest way in python to get out all the tuples from the
list whose first element is equal to i?


Use a list comprehension:
L = [(1,2,3), (2,3,1), (3,2,1), (1,2,5), (5,1,3)]
[t for t in L if t[0] == 1]

[(1, 2, 3), (1, 2, 5)]

yours,
Gerrit.

Jul 18 '05 #4
omission9:
Suppose I have a lists of tuples
What is the fastest way in python to get out all the tuples from the
list whose first element is equal to i?


A list is not an efficient data structure for this query. Why don't you
design another data structure?

--
René Pijlman
Jul 18 '05 #5

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

Similar topics

6
by: Narendra C. Tulpule | last post by:
Hi, if you know the Python internals, here is a newbie question for you. If I have a list with 100 elements, each element being a long string, is it more efficient to maintain it as a dictionary...
2
by: Kae Verens | last post by:
Anyone know of a script which returns a list of elements matching a specified CSS selector? Kae
2
by: David Pratt | last post by:
Hi. I like working with lists of dictionaries since order is preserved in a list when I want order and the dictionaries make it explicit what I have got inside them. I find this combination very...
5
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I...
22
by: Curious | last post by:
Hi, I am searching for a data structure that stores key-value pairs in it. This data structure is to hold large amounts of key-value pairs, and so needs to be efficient both in insertion and...
11
by: solandre | last post by:
hi there, i have to find matches in two sorted arrays. there are two good possibilities as a temporary solution, performing different, depending on the inner structure of the data. but i try to...
15
by: Macca | last post by:
Hi, My app needs to potentially store a large number of custom objects and be able to iterate through them quickly. I was wondering which data structure would be the most efficient to do this,a...
6
by: happyhondje | last post by:
Hello everyone, I've got a little issue, both programming and performance-wise. I have a set, containing objects that refer to other sets. For example, in a simple notation: (<a, b, c>, <d, e>)...
2
by: =?ISO-8859-1?Q?Marco_K=F6rner?= | last post by:
Hello, I'm working on mapping the car's environment by updating an occupancy grid. An occupancy grid dicretizes the 3D space in small grid elements (voxels). A grid element contains informations...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.