a=[('core_0_uart_0', 0, 'RBR'), ('core_0_uart_0', 18, 'DLH'), ('core_0_uart_0', 16, 'DLL'), ('core_0_uart_0', 10, 'LSR'), ('core_0_uart_0', 4, 'IIR'), ('core_0_uart_0', 6, 'LCR'), ('core_0_uart_0', 0, 'THR'), ('core_0_uart_0', 4, 'FCR'), ('core_0_uart_0', 2, 'IER'), ('core_0_uart_0', 8, 'MCR'), ('core_0_uart_0', 14, 'SCR'), ('core_0_uart_0', 24, 'PWREMU_MGMT'), ('core_0_timer_0', 0, 'tim'), ('core_0_timer_0', 8, 'tcr'), ('core_0_timer_0', 4, 'prd')] def sort_list(a): for i in range(len(a)): for j in range(i+1,len(a)): if a[i][0] == a[j][0]: if a[i][1]>a[j][1]: a[i],a[j] = a[j],a[i] res=[] res1=[] for i in a: i=list(i) res.append(i) [res1.append(res[i][0]) for i in range(len(res)) if res[i][0] not in res1] for i in res1: a=1 for k in res: if i in k: testcasenum="testcase{}_({})".format(str(a),i) k.insert(0,testcasenum) a+=1 return res b=sort_list(a) print(b) for i in b: print(i)