Home > Software engineering >  Store previous value of loop in python
Store previous value of loop in python

Time:06-14

I have a simple program in which I want to append elements in the append list based on if they match to the next element in the main list , i.e.

'''
-- Append the first element in the append list as it has no previous element 
-- After that check if the second element is same as first element 
-- if same: Do nothing , if different : Append to the append list 
-- The append list will be populated with all the values which don't match their previous values
'''

main_list = ['Apple', 'Apple', 'Apple', 'Apple', 
            'bat', 'bat', 'bat1', 'bat', 'cat', 'cat', 'cat', 
             'cat1', 'cat', 'cat', 'Apple', 'Apple', 'Apple', 
             'Apple']

append_list = ['']


previous_item = []
previous_item_2 = ['']
for item in main_list:
    previous_item.append(item)
    if previous_item[0] != previous_item_2[0]:
        append_list.append(item)
        previous_item_2 = []
        previous_item_2.append(item)

    else :
        pass 
    print(append_list)

With the current code the append list just gets populated with " " and "Apple"

CodePudding user response:

have a look at the output of

for prev_item, item in zip(main_list, main_list[1:]):
    print(prev_item, item)

and see if you can work it out from there

CodePudding user response:

keep tracking last element in append_list for every item in main_list

main_list = ['Apple', 'Apple', 'Apple', 'Apple', 
            'bat', 'bat', 'bat1', 'bat', 'cat', 'cat', 'cat', 
             'cat1', 'cat', 'cat', 'Apple', 'Apple', 'Apple', 
             'Apple']

append_list= list()

for item in main_list:
    l = len(append_list)
    if l == 0 or append_list[l-1] != item:
        append_list.append(item)
        
print(append_list)

# output
['Apple', 'bat', 'bat1', 'bat', 'cat', 'cat1', 'cat', 'Apple']

CodePudding user response:

main_list = ['', 'Apple', 'Apple', 'Apple', 'Apple', 'bat', 'bat', 
            'bat1', 'bat', 'cat', 'cat', 'cat', 'cat1', 'cat', 'cat']
append_list = []

for prev_item, item in zip(main_list, main_list[1:]):
    if prev_item != item:
        append_list.append(item)
    print(prev_item, item)
    print(append_list)

CodePudding user response:

res = [None]
lst = ['Apple', 'Apple', 'Apple', 'Apple', 
            'bat', 'bat', 'bat1', 'bat', 'cat', 'cat', 'cat', 
             'cat1', 'cat', 'cat', 'Apple', 'Apple', 'Apple', 
             'Apple']

for word in lst:
    if res[-1] != word:
        res.append(word)
print(res[1:])

Gives

['Apple', 'bat', 'bat1', 'bat', 'cat', 'cat1', 'cat', 'Apple']

CodePudding user response:

i found this to work especially well:

append_list = [item for i, item in enumerate(main_list) if main_list[i-1] != item or i == 0]

my understanding of how this works is that it iterates over every item in main_list and gives you access to the index as it does so, meaning you can check the item in main list with which is 1 element before the element you're currently iterating over and adding "item" eitehr if it isn't equal to said element or if the index is 0(it's the first element of the list)

Edit: If you wanted to add to the main list in a loop like

for item in ExampleList:
  main_list.append(item)

i suppose you could also add this to that same loop

  if item != append_list[-1]: #checks if the item is the same as the last entry to the append list
    append_list.append(item) #adds it to the append list

that's the best response i could give from your description of what you want to add but if this doesn't do what you want it to, just give a little bit of clarification or maybe an example and i can try to help you out some more

  • Related