Do you want to validate with Tuples, that is easy, making changes not easy.
In our recent video Python – how do I remove unwanted characters lists were used as a lookup to validate data that we need to be check for invalid data items. The most apparent difference between the two is that tuples are immutable, hence changing their values is not possible, making using them in real-time code a bit hazardous.
So why would you use Tuples?
That is a good question and sometimes not too obvious when you try to put examples down on paper, but here are some cases:
- You want a set of values that will never change, no matter what.
- Use as a lookup that the program can check against, these could be called anywhere in your code.
- Make sure that you only process what is in the tuple; any additional data can be reported as erroneous, a form of error control.
Getting around the change limitations (well kind of)
This video looks at a simple few steps to take in a set of data, validate the id column aginst a tuple set of values and then show the differences on a separate output.
The code is then rerun after we add the original tuple to the error values found, to give a new tuple. As a result, the new output will show up with no errors.
To sum it all up
In a nutshell, Tuples are limited in what they can do, probably the best thing for them is:
- Use your code as a reference for re-occurring values that need to be validated.
- Don’t use in your code to have updated tuples, use lists instead as you can update them in real-time.