+ 3
What is the Difference Between List and Tuple in Python ? what kind of collection are Store both ?
what kind of collection are Store both ? and Main Difference between than
6 Answers
+ 23
(Tuple)>>> immutable
[List ]>>> mutable
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2452/?ref=app
+ 6
I'll try to explain it briefly.
Both List and Tuple in Python are collection of data items, with the difference of whether it's mutable or not.
Mutable means it's changeable or simply, you can change its contents. Immutable is the opposite of mutable. An immutable data type means you can't change or modify its state or content after it has been created. The most notable example of an immutable data type is string, which you can't modify after its creation.
For List, it's mutable. We can see from the example below:
Firstly, we create a list x.
x = [1,2,3]
Since List is mutable, you can add, remove and modify item(s) in that list:
x.append(4) => [1,2,3,4]
x.remove(3) => [1,2,4]
x[0] = 5 => [5,2,4]
As contrary, a Tuple is an immutable type, meaning that you can't change or modify any of its elements after creation:
y = (1,2,3)
y.append(4) => Error
y.remove(4) => Error
y[0] = 5 => Error
If you take a further look to tuple's methods, it has comparably lesser methods than what a List has because of its immutable nature.
Another word, a Tuple is said to be read-only. The only thing you can do with Tuple is to obtain its value stored:
y[0] => 1
List is useful if you need to modify your data stored in the future, whereas Tuple is particularly useful if you want your data stored unable to modify and make it read-only data.
+ 6
For the problem of homogeneous and heterogeneous, you don't need to be too stress on the distinction for you to choose to use either list or tuple. For reason I'll try to explain below.
Homogeneous and heterogeneous are like what we said about the semantics of the data or type.
Homo = same
Hetero = different
In short, we can say that homogeneous is the data with same type and the opposite for heterogeneous. For example:
Homogeneous:
1 2 3 ... 10 => int
'hello' 'world' 'python' => str
1.0 2.0 3.0 ,,, 10.0 => float
Heterogeneous:
1 2.0 'three' => int + float + str
In tradition list is suitable for homogeneous and tuple is suitable for heterogeneous. This is because of the mutable and immutable state of list and tuple.
It makes sense for list to store the same type of data for adding or removing either of them in the future. For example, if we have a list
language_you_know = ['Python' ]
For now you only know Python. But if you learn another language in the future, let's say HTML, you would want to expand the list:
language_you_know = ['Python', 'HTML']
So it makes sense to store the same type or same category of data into list for the ease of modifying it.
For tuple, it usually useful for storing heterogeneous data like name of person, combination of details and so on. For example,
employee_A = (1, 'Alice')
This is an example of the detail of employee A. First detail is ID number, followed by name. You know that ID number and name of the employee will not change even many days later in the future, so you can store the details into tuple.
To conclude, even though list is homogeneous sequence and tuple is heterogeneous sequence, you can still store different data into list and same data type into tuple. It just a matter of tradition.
+ 1
okk, Andy Tan , thank you I got my answer by you
but I have One more doute ...
what is heterogeneous and
what is homogenous .? which are htro and which is homogenous collection .
and how I can say this is homogenous and this is heterogeneous ....
my atucal problem this both are...
for exmp .
list=[2,3,4,"hello",2.5]
I also able to define in a list
and same
tuple=(2,3,4,"hello",2.5)
I able to define in tuple also
???
+ 1
This is in no way related to C++, Java, Android and .NET. I don't know about the last 2 tags, but please, use the tag feature as intended.
0
noo , tag syntax ... no java syntax , no .net syntax ...
python used "Indentation "
Indentation we can say syntax of python .