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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With