Ticker

6/recent/ticker-posts

Diff b/w list and tuple in python

 Difference1

List:

List is comma seperated value in square bracket and bracket is mandatory.

L=[1,2,3]

Tuple:

Tuple is comma seperated value in parenthesis and parenthesis is optional.

T=(1,2,3)

Difference2

List is mutable.

L=[1,2,3]

L[1]=5

So updated list is

L=[1,5,3]


Tuple is immutable.

Difference3

Tuple object takes less memory than list object for same data.

L=['hello',12,23.5,2+3i]

T=('hello',12,23.5,2+3i)

sys.getsizeof(L)

Sys.getsizeof(T)

120

72

Tuple take 72 and list take memory 120.

Difference4

Comprehension concept is applicable only for list and not for tuple.

Difference5

List support packing not unpacking.

Tuple support packing and unpacking.

a=('hello',12,23.5,2+3i)

Unpack tuple

a,b,c,d=('hello',12,23.5,2+3i)

print(a)

hello






Post a Comment

0 Comments