Use of tuples | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Use of tuples

Could you give a simple real life example when tuple would be used over a list?

7th Nov 2016, 6:54 PM
Anastasia Borovkova
Anastasia Borovkova - avatar
5 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
7th Nov 2016, 8:11 PM
Burey
Burey - avatar
+ 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.
7th Nov 2016, 9:04 PM
clostry
+ 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:)
4th Dec 2016, 4:26 PM
Benedek Máté Tóth
Benedek Máté Tóth - avatar
+ 1
Cartesian or Geo coordinates would be a good use too
8th Nov 2016, 5:39 AM
Massamba Sow
Massamba Sow - avatar
+ 1
I seem... tuple: *elements of tuple are equal indices of dictionary *immutable *fast exemples: *row in table *constants true?
4th Dec 2017, 12:44 PM
Vitalij Bredun ♡ Python Petersburg
Vitalij Bredun ♡ Python Petersburg - avatar