How do I iterate through positions in a nested list?
Question:
I have a nested list here that shows the day in the first index position and the number of items in the second. I need to find whether the number of items on the previous day is less than the number of items on the current day and find the difference( e.g number of items on day 2 is less than day 1) and if in the 4 days all items are more than the previous days. But I am not sure how to iterate through the current figures. I expect to see the first statement get printed if items in current day is less than the previous day and the second if all days are more (Day: 2 Difference: 2, Day: 4 Difference: 1)
list1=[[1,3],[2,1],[3,4],[4,3]]
def calculator():
figure= list1[1][1]
for day,prev_figure in list1:
if figure<=prev_figure:
difference= prev_figure-figure
print(f"DAY:{day}, DIFFERENCE{difference}")
for day,prev_figure in list1:
if figure>=prev_figure:
print("EVERY DAY ITEMS ARE MORE THAN THE PREVIOUS DAY")
Answers:
If I understand your req. clearly, you can try this and ask any questions:
list1=[[1,8],[2,5],[3,4],[4,3]] # Note: each day's item less than previous day's
prev_day, prev_item = list1[0] # use unpacking to set the first day
count = 0
for curr_day, curr_item in list1[1:]:
if curr_item < prev_item:
diff = prev_item - curr_item
print(f"DAY:{curr_day}, DIFFERENCE: {diff}")
prev_item = curr_item # reset it to the current one
count += 1
if count == len(list1) -1:
print("Every Day ITEMS Are Less than Previous Day!")
Output:
DAY:2, DIFFERENCE: 3
DAY:3, DIFFERENCE: 1
DAY:4, DIFFERENCE: 1
EVERY DAY ITEMS ARE MORE THAN THE PREVIOUS DAY
Edit – for checking only previous day item greater than current day. as OP’s requirement!
list1 = [[1,3],[2,1],[3,4],[4,3]]
def calculator(L):
prev_day, prev_item = L[0]
count = 0
for curr_day, curr_item in L[1:]:
if curr_item < prev_item: # less than
diff = prev_item - curr_item
print(f"DAY:{curr_day}, DIFFERENCE: {diff}")
count += 1
prev_item = curr_item
if count == len(L) -1:
print("Every day items are different than Previous day!")
calculator(list1)
Output2:
DAY:2, DIFFERENCE: 2
DAY:4, DIFFERENCE: 1
I have a nested list here that shows the day in the first index position and the number of items in the second. I need to find whether the number of items on the previous day is less than the number of items on the current day and find the difference( e.g number of items on day 2 is less than day 1) and if in the 4 days all items are more than the previous days. But I am not sure how to iterate through the current figures. I expect to see the first statement get printed if items in current day is less than the previous day and the second if all days are more (Day: 2 Difference: 2, Day: 4 Difference: 1)
list1=[[1,3],[2,1],[3,4],[4,3]]
def calculator():
figure= list1[1][1]
for day,prev_figure in list1:
if figure<=prev_figure:
difference= prev_figure-figure
print(f"DAY:{day}, DIFFERENCE{difference}")
for day,prev_figure in list1:
if figure>=prev_figure:
print("EVERY DAY ITEMS ARE MORE THAN THE PREVIOUS DAY")
If I understand your req. clearly, you can try this and ask any questions:
list1=[[1,8],[2,5],[3,4],[4,3]] # Note: each day's item less than previous day's
prev_day, prev_item = list1[0] # use unpacking to set the first day
count = 0
for curr_day, curr_item in list1[1:]:
if curr_item < prev_item:
diff = prev_item - curr_item
print(f"DAY:{curr_day}, DIFFERENCE: {diff}")
prev_item = curr_item # reset it to the current one
count += 1
if count == len(list1) -1:
print("Every Day ITEMS Are Less than Previous Day!")
Output:
DAY:2, DIFFERENCE: 3
DAY:3, DIFFERENCE: 1
DAY:4, DIFFERENCE: 1
EVERY DAY ITEMS ARE MORE THAN THE PREVIOUS DAY
Edit – for checking only previous day item greater than current day. as OP’s requirement!
list1 = [[1,3],[2,1],[3,4],[4,3]]
def calculator(L):
prev_day, prev_item = L[0]
count = 0
for curr_day, curr_item in L[1:]:
if curr_item < prev_item: # less than
diff = prev_item - curr_item
print(f"DAY:{curr_day}, DIFFERENCE: {diff}")
count += 1
prev_item = curr_item
if count == len(L) -1:
print("Every day items are different than Previous day!")
calculator(list1)
Output2:
DAY:2, DIFFERENCE: 2
DAY:4, DIFFERENCE: 1