Python Forum
Access the full list using an api that rather returns only a few elements
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Access the full list using an api that rather returns only a few elements
#1
How do I access the full list defined in another function with an api call that rather returns only a top 10 elements that starts with a given sub-string supplied to the api call in the current function.

def currentFunc(api):
   #function returns ['banana', 'bananas', 'ban'] but needs the whole lst ...
   #['apple', 'banana', 'bananas', 'ban', 'apple phone', 'apple ipad'] returned.
   lst = api('ba') #'ba' is chosen for demonstrating the function of api.
   return lst

def listFunc():
    lst = ['apple', 'banana', 'bananas', 'ban', 'apple phone', 'apple ipad']
    api = lambda str: [l for l in lst if l.startswith(str)][:10]
    assert currentFunc(api) == lst
Say our function in focus is CurrentFunc. This function has a parameter, api, passed as defined in the function listFunc. When any string is further passed to this api function, for instance, 'ba' it returns a maximum of 10 matching values from the list lst that start with 'ba' namely ['banana', 'bananas', 'ban']. But I want the function currentFunc return not just these values but the whole of 'lst' defined in the function listFunc.

I was wondering what would be the logic to apply here to get the full list through this rather limited window of the api call. The number of api calls must be as few as possible though for computation reasons.
Reply
#2
It is an impossible task without further assumptions. Suppose that for some n > 10
lst = ['apple'] * n + ['banana'] * n
the only possible return values for api() in this case are an empty list, or 10 times apple or 10 times banana. This makes it impossible to find n, hence lst.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 413 Jan-27-2024, 04:03 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 460 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Access list of dictionaries britesc 4 1,063 Jul-26-2023, 05:00 AM
Last Post: Pedroski55
  Checking if a string contains all or any elements of a list k1llcod3 1 1,081 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  How to change the datatype of list elements? mHosseinDS86 9 1,950 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  function returns dataframe as list harum 2 1,386 Aug-13-2022, 08:27 PM
Last Post: rob101
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,098 May-17-2022, 11:38 AM
Last Post: Larz60+
  Why am I getting list elements < 0 ? Mark17 8 3,107 Aug-26-2021, 09:31 AM
Last Post: naughtyCat
  Looping through nested elements and updating the original list Alex_James 3 2,111 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  Extracting Elements From A Website List knight2000 2 2,244 Jul-20-2021, 10:38 AM
Last Post: knight2000

Forum Jump:

User Panel Messages

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