What is the difference between a list and a tuple? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the difference between a list and a tuple?

4th Oct 2019, 11:38 AM
Vimuth
Vimuth - avatar
2 Answers
+ 11
► Syntax for creating a list : list_demo = [1,2,3,4] which is surrounded by square brackets while syntax for creating a tuple : tuple_demo = (1,2,3,4) which is surrounded by round brackets. ► You can check the list of available functions in the both of them by using dir() which is an inbuilt function. Use dir(list_demo) and dir(tuple_demo), you'll clearly understand the difference in their features. (If you carefully observed, then lists have more functions as compared to tuples) ► The most important difference is that the lists are mutable in nature (they can be modified after creating them) while tuples are immutable (they cannot be modified after their creation). Try list_demo[1] = 8 and print(list_demo). Do same with tuples, tuple_demo[1] = 8 and print(tuple_demo), here you'll be getting an error. Hope this was sufficient for you to understand the difference !!
4th Oct 2019, 11:49 AM
Nova
Nova - avatar
+ 3
Thank you both of you
4th Oct 2019, 12:01 PM
Vimuth
Vimuth - avatar