I have a django list as below: the hotess1 is a database result set.
for hotel in hotels1:
if models.CalendarDay.objects.filter(hotel=hotel, date=date).count() == 0:
similar_venues.append(hotel)
The list size is dynamic but i want to return only first three values
I can do as below:
for hotel in hotels:
if models.CalendarDay.objects.filter(hotel=hotel, date=date).count() == 0:
similar_venues.append(hotel)
counter += 1
if counter == 3: break
but i want to do it in a better way.. any help
You can use the slice operator:
my_list[:3]will return a list containg the first three values ofmy_list.(unless there are less than three values, in which case you get everything back).