Home > database >  difference between lists and tuples in python
difference between lists and tuples in python

Time:12-11

wants to know about the difference between lists and tuples in python language. their syntax and their use

I searched several websites to know the difference


CodePudding user response:

Tuple syntax:

variable = (filling(ex. (1,2,3,4,5,'6'... ) ))

List syntax:

variable = [filling(ex. [1,2,3,4,5,'6'...]) ]

The main differences, briefly:

  • The literal syntax of tuples is shown by parentheses () whereas the literal syntax of lists is shown by square brackets [] .
  • Lists has variable length, tuple has fixed length.
  • List has mutable nature, tuple has immutable nature.
  • List has more functionality than the tuple.

More information here:

https://realpython.com/python-lists-tuples/

CodePudding user response:

List and tuple act as containers for storing objects. But there is a difference in its use cases and syntax as well.

Lists are surrounded by square brackets [ ] while tuples are surrounded by round brackets ( ).

Creating a list and tuple in python.

list_numbers = [1,2,3,4,5]

tuple_numbers = (1,2,3,4,5)

print(list_numbers)

print(tuple_numbers)

We can use the type function to check the data type of any object.

type(list_numbers)

type(tuple_numbers)

One more thing.... list can be modified however tuples can not be modified and hence are called immutable container

CodePudding user response:

The primary difference between tuples and lists is that tuples are immutable as opposed to lists which are mutable. Therefore, it is possible to change a list but not a tuple.

The contents of a tuple cannot change once they have been created in Python due to the immutability of tuples.

There is no way to keep changing tuples. Error message if you attempt to change one of the items:

names = ("Raj","John","Jabby","Raja")

names[2] = "Kelly"

Traceback (most recent call last):

File "", line 4, in

TypeError: 'tuple' object does not support item assignment

If you're familiar with lists and maps, you know that they can be modified. You can add or remove items or reassign them to different variables. But tuples? Well, you can't do any of that.

The reason is simple: tuples are immutable, meaning you cannot change their contents after they are created. The length of tuples is also fixed. They remain the same length throughout the lifecycle of the program.

CodePudding user response:

0

The primary difference between tuples and lists is that tuples are immutable as opposed to lists which are mutable. Therefore, it is possible to change a list but not a tuple.

The contents of a tuple cannot change once they have been created in Python due to the immutability of tuples.

There is no way to keep changing tuples. Error message if you attempt to change one of the items:

names = ("Raj","John","Jabby","Raja")

names[2] = "Kelly"

Traceback (most recent call last):

File "", line 4, in

TypeError: 'tuple' object does not support item assignment

If you're familiar with lists and maps, you know that they can be modified. You can add or remove items or reassign them to different variables. But tuples? Well, you can't do any of that.

The reason is simple: tuples are immutable, meaning you cannot change their contents after they are created. The length of tuples is also fixed. They remain the same length throughout the lifecycle of the program.

CodePudding user response:

Sr. No. Key List Tuple 1 Type List is mutable. Tuple is immutable. 2 Iteration List iteration is slower and is time consuming. Tuple iteration is faster. 3 Appropriate for List is useful for insertion and deletion operations. Tuple is useful for readonly operations like accessing elements. 4 Memory Consumption List consumes more memory. Tuples consumes less memory. 5 Methods List provides many in-built methods. Tuples have less in-built methods. 6 Error prone List operations are more error prone. Tuples operations are safe.

  • Related