Skip to main content
Question

Get first item from search

  • May 22, 2025
  • 1 reply
  • 5 views

Forum|alt.badge.img

Is it possible to retrieve the first item in one line without looping over it and breaking? 

items1 = client.search().query(query="22",limit=1, ancestor_folder_ids=["159631784184"], sort="modified_at")
for item in items1:
print(f'The item ID is {item.id} and the item name is {item.name}')
item1 = item.id
break

But, just doing: item1 = items1[0]

Returns: TypeError: 'LimitOffsetBasedObjectCollection' object is not subscriptable

 

1 reply

Forum|alt.badge.img

Hi!
query() method returns BoxObjectCollection object, which inherits from Iterator. Therefore you can use either next() method exposed by BoxObjectCollection class or next() method inherited form Iterator class. To get first item you can do:

item1 = items1.next() # recommended 

or

item1 = next(items1)

Regards