Python Forum
Best practices for searching lists of lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best practices for searching lists of lists
#1
Hi, all,

Python 3.9, compiled from source on OSX 10.13.6

After a fair amount of experimentation and searching the inter-tubes, I have a few questions about searching lists of lists based on partial matches to the list items.

Sorry for the earlier indentation issue. Was apparently not using the bbcode properly...

Assuming:
my_list = [{'name': ['myvar', 'val1', 'val2'], 'id': '181'}, {'name': ['val3'], 'id': '9205'},  {'name': ['val4a', 'val5'], 'id': '44256'}]

search = 'val5'

def my_search(search, entries):
    """ Return a dictionary entry upon matching criteria, or None """
    result = None
    search = search.lower()
    for i in entries:
        if [k for k in [i.lower() for i in i['name']] if search in k]:
            result = i
            break
    return result
Searching for 'val4' would return the 3rd dictionary entry, i.e.,
>>> print(f"{my_search('val4', my_list)}")
>>> {'name': ['val4a', 'val5'], 'id': '44256'}

I do want the entire dictionary returned as there may be other key/value pairs in each dictionary entry, and additional processing based on their contents.

The questions:
  • Is there a way to reduce code in the function by employing another list comprehension, for 'i', or is this the best method for searching the dictionary entries? I'd like to get it down to one line if possible, without the outer 'for i in entries:' loop. (For extra credit, would love to be able to just return the result of a single comprehension, or None, as needed.)
  • The dictionary may grow, but is currently fairly small, say a couple of dozen dozen entries with each target search list containing one to six or so items. Is it recommended, performance-wise, to force the search to be lower case before iterating the dictionary entries, or is it just as good having it be in the comprehension with search.lower() instead? The former would seem to be preferable as it only has to do the conversion once, rather than for each list being searched. The search itself does need to be lower-case so as to standardize what's being evaluated, or it might not match ('Val3' vs 'val3', etc.).
  • Is there a better way to search the list items in a case-insensitive manner without first creating 'j' (the lower-case list for each dictionary entry)? Doing it this way means I'm effectively iterating each dictionary entry's list twice, but either it has to be lower case as mentioned above, or all the list items need to be lower and that could complicate displaying a 'prettyfied' search result.
  • Is there a better method for returning the target dictionary entry, or do I need to store it in 'result' as I'm doing now and return it when the search is successful?

(Getting the comprehension to iterate each dictionary's list items in one pass has been troublesome. If I specify the index of the list item in the comprehension, it works, but then I'm stuck just searching that one entry in the list, not all the items it contains.)

Any pointers would be most welcome. Thanks!
Reply
#2
please fix code and place in python tags
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with print lists MarekGwozdz 4 613 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
  python convert multiple files to multiple lists MCL169 6 1,436 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Lists blake7 6 693 Oct-06-2023, 12:46 PM
Last Post: buran
  Trying to understand strings and lists of strings Konstantin23 2 699 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Why do the lists not match? Alexeyk2007 3 764 Jul-01-2023, 09:19 PM
Last Post: ICanIBB
  ''.join and start:stop:step notation for lists ringgeest11 2 2,381 Jun-24-2023, 06:09 AM
Last Post: ferdnyc
  Need help with sorting lists as a beginner Realist1c 1 712 Apr-25-2023, 04:32 AM
Last Post: deanhystad
  Pip lists the module but python does not find it Dato 2 1,232 Apr-13-2023, 06:40 AM
Last Post: Dato
  Generate lists of devices and partitions from /proc/partitions? DachshundDigital 1 730 Feb-28-2023, 10:55 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020