0

On Jupyter - I have a complex nested for loop and if clauses and a list_pairs:

pair_list = [[1,2], [1,3], ...] --> len(pair_list) about 3.5 million

check = 0 

for i, j in pair_list:
   print(check) #this creates one extra line for each iteration
   check += 1 

these outputs:

0
1
2
3
4

is it possible to print the check variable each second with the updated number - for example 0 should be replace with 1 and 1 with 2 -- it should print on the same line each time and replace the old with the new one - it can also be outside the for loop

Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48
TheDev
  • 153
  • 5
  • 1
    Does this answer your question? [Output to the same line overwriting previous output?](https://stackoverflow.com/questions/4897359/output-to-the-same-line-overwriting-previous-output) – Yevhen Kuzmovych May 24 '21 at 13:20

2 Answers2

1

What you need is a "progress bar"!

How to get it? Use tqdm. For jupyter-notebook, use this

from tqdm.notebook import tqdm

for i, j in tqdm(pair_list):
   #You won't need check variable
   print(check) #this creates one extra line for each iteration
   check += 1 
   # Carry on with the logic

Sample usage:

enter image description here

paradocslover
  • 2,932
  • 3
  • 18
  • 44
0

In Python you can change the value of an item in a list using list indexing,

pair_list = [[1,2], [1,3], ...]

check = 0 

for i in range(len(pair_list)):
   pair_list[i][0] = pair_list[i][1]
   check += 1
   print(check)

par_list[i][0] would index the 0th element in the Ith list. Then par_list[i][1] would be indexing the second element in the Ith list. list indexing always starts at 0, so 0 is first, 1 is second item and so on.