#!/usr/local/bin/python3 s = 'yamada' b = [('suzuki', 'satou'), ('yamada', 'tanaka'), ('tanaka', 'satou'), ('watanabe', 'suzuki')] ans = [] cur = s ans.append(cur) while b: for i, pair in enumerate(b): if cur in pair: break del b[i:i+1] cur = pair[1] if pair[0] == cur else pair[0] ans.append(cur) print(ans)