I am trying to connect values in tuples in a list without using a dictionary. Specifically, I have this list:
adjList = [('0', '3'), ('1', '0'), ('3', '2'), ('4', '2'), ('5', '4'), ('7', '9'),
('8', '7'), ('9', '6'), ('2', '1'), ('2', '6'), ('6', '5'), ('6', '8')]
and I would like to create a list with the values from a random tuple:
newList = ['1', '0']
then append the second value of a tuple from adjList if the first value of that tuple is the same as the last value of newList, thus:
newList = ['1', '0', '3']
Then delete ('1', '0') and ('0', '3') from adjList.
THEN I want to repeat this action until the last value in newList NO LONGER corresponds to the first value of a tuple from adjList. I am having a lot of trouble figuring out a logical combination of while or for loops that can do this, and any help would be appreciated.
My code so far:
adjList = [('0', '3'), ('1', '0'), ('3', '2'), ('4', '2'), ('5', '4'), ('7', '9'),
('8', '7'), ('9', '6'), ('2', '1'), ('2', '6'), ('6', '5'), ('6', '8')]
firstNode = random.choice(adjList)
newList = []
newList.append(firstNode[0])
newList.append(firstNode[1])
adjList.remove(firstNode)
## I need to repeat the following block of code:
for ax,bx in adjList:
if newList[-1] == ax:
adjList.remove((ax,bx))
newList.append(bx)
break
Everything works the way it should, but of course I am only getting 3 values in newList at the end. I can't quite work out how to repeat that final block of code until I run out of tuples in adjList.
Thanks in advance for any help.
adjList? Let's say that randomly selected tuple is('9', '6')what would be the correct value ofnewList?