I have a list of dicts that can contain from 0 to 100 elements. I want to view only the first three elements, and I do not want to throw an error if the list has less than three elements. How to do this in python?
psuedocode:
for element in my_list (max of 3):
do_stuff(element)
EDIT: This code works, but feels very unclean. I feel that python has a better way to do this:
counter = 0
while counter < 3:
if counter >= len(my_list):
break
do_stuff(my_list[counter])
counter += 1
source
share