Python Get Last Item of Iterator

April 24, 2019
item = None
for item in items:
    pass

last_item = item

or

last_item = list(items)[-1]

NOTE: This solution will consume more memory (convert iterator to list).

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.