I’m posting below a problem I solved, but I feel there is a smoother, more elegant way to do it. If you don’t mind, please look at my code and tell me where I can fine-tune it, make it prettier.
*The prompt: Write a Python program that accepts a list *
of integers and calculates the length and the fifth element.
*Return true if the length of the list is 8 and the fifth element *
occurs thrice in the said list.
var1 = [19, 19, 15, 5, 5, 5, 1, 2]
var2 = [19, 15, 5, 7, 5, 5, 2]
count = 0
if len(var1) == 8:
fifth_el = var1[4]
for n in var1:
if n == fifth_el:
count = count + 1
if count == 3:
print("True")
else:
print("False")