Python Tuple vs List

Python-Tuple-vs-List-ipcisco.com

The Differences Bwtween Python Tuples and Lists

 

Python tuples and Python lists are two data typess used in python. Each of these data types is used for differetn purposes. They have some common properties and some differences. In this lesson, we will focus on Python Tuple vs List and we will learn the differences of these two python data types.

 

So, what are the differencesof Python Tuples and Lists? Let’s see each of them one by one.

 


 

Different Syntax

 

Tuples and lists used in python has different syntax. For python tuples, we use round paranthesis. But for python lists, we use square brackets. Between the members of both the tuples and lists, we use comma.

 

Let’s compare Python Tuple vs List  and give an example to understand this difference. The first one is an example of a python tuple and the second one is an example of a python list.

 

numbertuple = (1,2,3,4,5)
print(numbertuple)
(1, 2, 3, 4, 5)

 

 

numberlist = [1,2,3,4,5]
print(numberlist)
[1, 2, 3, 4, 5]

 

 

 

As you can see above, python tuple has created with round paranthesis, python lists has created with square brackets.

 


 

Creation

 

The other diference of python tuples and python lists is about the creation. We can create a tuple in three ways like below:

 

numbertuple = (1,2,3,4,5)
print(numbertuple)

 

numbertuple = 1,2,3,4,5
print(numbertuple)

 

numbertuple = tuple((1,2,3,4,5))
print(numbertuple)

The output of this codese will be same:

 

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

 

 

 

 

To create a python list, we can use two methods.

 

numberlist = [1,2,3,4,5]
print(numberlist)

 

numberlist = list((1,2,3,4,5))
print(numberlist)

 

The outputs of these example will be same:

 

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

 

 


 

Mutability

 

Mutability is one of the key differences if we compare Python Tuple vs List.   Python tuples are not mutable, in other words they are immutable. We can not change a python tuple. They are defined and stay unchange in the code.

 

On the other hand, python lists are mutable. We can change, modify a python list in the code. We can add new members, we can remove memebrs etc.

 

Let’s do an example to show this difference. Below, for the first code, we will try to add a new member to a tuple.

 

numbertuple = list(1,2,3,4,5)
numberlist.append(10)
print(numbertuple)

 

The output of this code will be error. Because we can not modify python tuples.

 

Now, let’s try to add a new member to a python list and change it.

 

numberlist = [1,2,3,4,5]
numberlist.append(10)
print(numberlist)

 

As you can see below, our list has changed and new member has added to the list.

 

[1, 2, 3, 4, 5, 10]

 


Speed

 

The other differenxe of these two data type is speed. If we compare Python Tuple vs List, tuples are faster than lists. In a small codes we can not understand these speed but for long processes, this speed can be important.

 

So, if you need more speed, you can use python tuples. If speed is not important for your code, you can use python lists.

 


 

Memory

 

Like speed, memory usage is also important sometimes. If you have large code, this usage become more important. So, the memory usage of these two data types are different.

 

Tuples uses less memory than lists. And lists uses more memory than tuples.

 


 

Usability With Dictionaries

 

In python, we can use tuple objects as keys for python dictionaries. But we can not use list objects as keys for dictionaries. Because, keys must be immutable and hashable.

 


 

Methods

 

Python tuples and python tuples uses some common and different methods. And python lists uses more methods than python tuples.

 


 

Last Word For Python Tuple vs List

 

In this Python Tuple vs List lesson, we have list the differences of these two python data types. According to your need, you can use one of these data types or you can use them together in your code.

 

Back to: Python Programming Course > Python Tuples

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact

  • info@ipcisco.com
IPCisco is the Winner! “Best Certification Study Journey of 2019!”

Cisco-ITBlogAwards-2019-Winner-IPCisco-k