Issue1548178
This issue tracker has been migrated to GitHub,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2006-08-28 20:47 by kovan, last changed 2022-04-11 14:56 by admin. This issue is now closed.
Messages (6) | |||
---|---|---|---|
msg54883 - (view) | Author: kovan (kovan) | Date: 2006-08-28 20:47 | |
In the benefit of Python's readability and simplicity, a 'find' method could be added to sequence types, that would return the first item in the list that matches some criteria. For example, it's a common practice to use lists of (key,value) pairs instead of dictionaries when the sequence must be ordered. To find an element maching a key in such cases, I frequently find myself writing (IMHO) too much code for such a straightforward operation. AFAIK currently there are two easy ways to do this (shouln't be one, and only one?): for item in items: if item.attribute == key: foundItem = item break else: foundItem = None OR foundItems = [item for item in items if item.key == value] if foundItems: foundItem = foundItem[0] IMO, in none of the cases the code is as clear and, specially, as short, as it should be. With the find method, the same code would be: item = items.find(lambda x: x.key == value) |
|||
msg54884 - (view) | Author: Josiah Carlson (josiahcarlson) * | Date: 2006-09-03 19:07 | |
Logged In: YES user_id=341410 Lists have an .index(obj[, start[, stop]]) method that tells you the index of the first object that matches obj, raising an exception otherwise. Generally speaking, you can get better performance for repeated searches by... dct = {} for i,(k,v) in enumerate(lst): dct.setdefault(k, []).append(i) Then to find the first index... dct.get(k, [None])[0] Suggested close. |
|||
msg54885 - (view) | Author: Georg Brandl (georg.brandl) * | Date: 2006-09-06 06:17 | |
Logged In: YES user_id=849994 Looking at all those efforts to remove str.find(), I think this is not going to happen. The sequence API is already rich enough. |
|||
msg54886 - (view) | Author: kovan (kovan) | Date: 2006-09-06 09:10 | |
Logged In: YES user_id=1426755 It's not about performance, it's about code readability and simplicity for such a common operation. The index() method is useless here because, as I explained, it's not a search by value. |
|||
msg54887 - (view) | Author: Georg Brandl (georg.brandl) * | Date: 2006-09-06 09:18 | |
Logged In: YES user_id=849994 What should find() return if nothing is found? It can't return None since that can be a valid item, so it has to raise an exception. Therefore, try: item = items.find(lambda x: x.key == value) except WhateverError: handle is not much shorter than try: item = (x for x in items if x.key == value).next() except StopIteration: handle |
|||
msg54888 - (view) | Author: kovan (kovan) | Date: 2006-09-06 09:25 | |
Logged In: YES user_id=1426755 Oh OK, I hadn't thought about the fact that None can be a valid item in the sequence. |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-11 14:56:19 | admin | set | github: 43905 |
2006-08-28 20:47:11 | kovan | create |