In Python, even if the variable contains a list, it should be used as a regular variable like index = []
. When you use [index] = []
Python thinks you want to assign the first item of the list to your variable.Likewise, when you use the code [first, last] = [1,2]
, then the variable first
is assigned 1 and the variable last
is assigned 2. This is called unpacking.
Also, in [index].append(2)
, you should not use the square brackets around the variable name. This would not raise any errors, but what it would do is create a new list (with the sole item being the value of index
) and then destroy the list when the line has executed.
Your code should look like this (Assuming the other parts of your code are correct). Also, as this comment suggested, use c
instead of c != []
because an empty list is false and it follows Python conventions. :
tmp = c.pop(0) # taking out the first element from the list as the starting point
complete = tmp[0][-1]
print(complete)
while c: # c != []
complete += tmp[1][-1]
index = [] # [index] = []
for i, pair in enumerate(c):
if pair[0] == tmp[1]:
index.append(i) # [index].append(i)
temp = c.pop(index)
print(complete)
As noted in the comments, the line temp = c.pop(index)
will give an error because pop expects an integer and the code is giving it a list.
However, because of the OP's usage of index
in the code, I think he meant to use index as a integer. Also, the OP stated that the usage of temp
instead of tmp
was a mistake.
tmp = c.pop(0) # taking out the first element from the list as the starting point
complete = tmp[0][-1]
print(complete)
while c:
complete += tmp[1][-1]
index = 0
for i, pair in enumerate(c):
if pair[0] == tmp[1]:
index = i
tmp = c.pop(index)
print(complete)