+ 5
Use of tuples
Could you give a simple real life example when tuple would be used over a list?
6 Answers
+ 6
simplest i can think of is a tuple of constant values which will be initialized at the start of a script and you do not wish for them to change
another different example can be seen here:
checking if a file name ends with one of the extensions which are in the tuple/list
'myDoc.txt'.endswith(('.docx', '.txt')) will return True value
'myDoc.txt'.endswith(['.docx', '.txt']) will return a TypeError and will not even run
+ 3
Creating tuples is faster than creating lists, so it's usually better to use tuples when possible.
Suppose you have a file containing multiple columns of data to analyze, a good solution may be to read it as a list of tuples, where every tuples is a row.
+ 3
For example you build a game, where all warrior has a name, and a race. these features doesnt expected to be changed during the game, so it is better to put them in tuples, as tuples are immutables. once created, you cannot change it, just using the datas.
or you build an app where persons and their birtydays are used. these data are not supposed to be changed, so better to store them in tuples.
just some examples, sorry my english:)
+ 1
Cartesian or Geo coordinates would be a good use too
+ 1
I seem...
tuple:
*elements of tuple are equal indices of dictionary
*immutable
*fast
exemples:
*row in table
*constants
true?