Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lazy slice in python3

Tags:

python

list

I have a huge list (~1_800_000 items) in python which is constructed using map() function from about 1000 JSON files. I want to check few first items to be sure that script is working correctly. I'm doing it like this:

items = map(lambda file: load_json(file), file_list)
print(list(items)[:5])

Converting map to list takes about 5-10 seconds, is it possible to take few first items without converting map result to list?

like image 222
Kirill Avatar asked May 06 '26 04:05

Kirill


1 Answers

You can do:

items = map(lambda file: load_json(file), file_list)
print([next(items) for _ in range(5)])

Or use itertools.islice, which has the slight advantage that it will not fail if you have less than five items:

items = map(lambda file: load_json(file), file_list)
print(list(itertools.islice(items, 5)))

Note that both of these consume the first elements in items, so if you want to "peek" these elements and then get the whole list, you will need to preppend these items first.

like image 151
jdehesa Avatar answered May 09 '26 01:05

jdehesa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!