3

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?

2
  • 3
    itertools.islice? Commented Aug 27, 2019 at 11:10
  • 2
    Unrelated, but you don't need to create a lambda if you are going to pass its argument to another function. you can directly pass the function to map: items = map(load_json, file_list) Commented Aug 27, 2019 at 11:17

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.