Home > Software engineering >  Getting the last two variables in for loop
Getting the last two variables in for loop

Time:05-02

I am trying to make a program that shows me the data of two specific coins. What it basically does is to takes the data in an infinite "for loop" to display the info until I close the program.

And now I am trying to get the last two elements of this infinite for loop every time it runs again and make calculations with it. I know I can't just hold all the items in a list and I am not sure how to store last two's and use them every time.

for line in lines:
    coinsq = line.strip()
    url = priceKey   coinsq   "USDT"
    data = requests.get(url)
    datax = data.json()
    print( datax['symbol']   " "   datax['price'])

CodePudding user response:

You can just use two variables that you update for each new elements, at the end you will just have the two last elements seen :

pre_last = None
last = None
for line in lines:
    coinsq = line.strip()
    url = priceKey   coinsq   "USDT"
    data = requests.get(url)
    datax = data.json()
    print( datax['symbol']   " "   datax['price'])
    pre_last = last    
    last = datax
#Do the required calculations with last and pre_last

(And just to be exact this isn't an infinite loop otherwise there wouldn't be a 'last' element)

CodePudding user response:

As your script does not have prior information of when the execution is going to halt, I suggest to define a queue-like structure. In each iteration, you update your last item and your previous-to-last. In that way, you just have to keep in memory two elements. I don't know how were you planning on accessing those two elements when the execution has finished, but you should be able to access that queue when the execution is over.

Sorry for not providing code, but this can be done in many ways, I supposed it was better to suggest you a way of proceeding.

CodePudding user response:

Store the data in a deque (from the collections module).

Initialise your deque like this:

d = deque([], 2)

Now you can append to d as many times as you like and it will only ever have the most recent two entries.

So, for example:

d.append('a')
d.append('b')
d.append('c')

for e in d:
    print(e)

Will give the output:

b
c

Adapting your code to use this technique should be trivial.

I recommend this approach in favour of using two variables because it's easier to change if you (for some reason) decided that you want the last N values because all you need to do is change the deque constructor

CodePudding user response:

You can define a variable for the second-last element of the for loop, and use the datax variable that's already defined in the loop as the last element:

sec_last = None
datax = None
for line in lines:
    sec_last = datax
    coinsq = line.strip()
    url = priceKey   coinsq   "USDT"
    data = requests.get(url)
    datax = data.json()
    print( datax['symbol']   " "   datax['price'])

print(f"Last element", datax)
print(f"Second Last element", sec_last)
  • Related