curses is the go-to for terminal displays.
#!/usr/bin/env python
import curses
import time
def updater():
fakedata = [[1, 78], [2, 97], [1, 45], [2, 2], [3, 89]]
codes_to_linenums_dict = {}
last_line = 0
win = curses.initscr()
for code, data in fakedata:
try:
# if we haven't seen this code before, give it the next line
if code not in codes_to_linenums_dict:
codes_to_linenums_dict[code] = last_line
last_line += 1
# use the line we set for this code.
line = codes_to_linenums_dict[code]
win.addstr(line, 0, '{}: {} '.format(code, data))
win.refresh()
# For display only
time.sleep(2)
except KeyboardInterrupt:
# if endwin isn't called the terminal becomes unuseable.
curses.endwin()
raise
curses.endwin()
if __name__ == '__main__':
updater()
~