Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update tuple in list

Ok, so I want to do something like:

if title and articleNumber in listOfData:
    listOfData[articleNumber].update(title, articleNumber, None, oldvariants + variants)
else:
    listOfData.append((title, articleNumber, None, variants))

I realize that the syntax is not correct, but since I don't know what I want to do, I described it as the syntax I would want to use :) The article number is a large integer (i don't know if it can start with 0)

Note that I don't want to use a dictionary, since listOfData should be the same format as other parts of the code. (This is unfortunate, because the articles i'm parsing is divided on several rows in some cases which makes me need this check if it is in the list already)

like image 934
Viktor Mellgren Avatar asked May 03 '26 22:05

Viktor Mellgren


1 Answers

You cannot update a tuple in place, by its definition. Its immutable. You can however replace it completely.

if title and articleNumber in listOfData:
    listOfData[articleNumber] = (title, articleNumber, None, oldvariants + variants)
else:
    listOfData.append((title, articleNumber, None, variants))

update is a dictionary method to add an entry which makes me think that you might be trying to have one entry in list of data consist of more than one tuple. If this is the case you'll need a different approach.

like image 96
Ryan Haining Avatar answered May 05 '26 11:05

Ryan Haining



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!