Reputation: 37
def run(lst, tup):
tup_1 = ()
for x in range(len(lst)):
tup_1[x] = lst[x]
if tup_1[1] == lst[1]:
return True
for y in range(len(tup_1)):
if tup_1[y] == tup[y]:
return "matched"
else:
return "not equal"
print run([1,2,3],(1,2,3))
I tried to convert number in List to Tuple form, so I can compare with another tuple. But the problem is it returns an error like this:
Traceback (most recent call last): File "", line 16, in File "", line 5, in run TypeError: 'tuple' object does not support item assignment
Upvotes: 1
Views: 4275
Reputation: 13723
This occurs because tuples are immutable, data inside them cannot be mutated
example:
>>> t = (1,2,3)
>>> t[1] = 4
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
Unlike lists which are mutable:
>>> l = [1,2,3]
>>> l[1] = 4
>>> l
[1, 4, 3]
So use a list instead of a tuple:
tup_1 = []
If you need to convert a list to a tuple you can use the built-in tuple()
function
For more information I recommend you read the documentation
Upvotes: 1
Reputation: 239573
You can convert a list to tuple with tuple
function like this
tuple(lst)
And to check if a list and tuple are the same, you can simply do this
return tuple(lst) == tup
Upvotes: 1