Sunteți pe pagina 1din 7

12/10/2017 Python: Lists vs.

Tuples Arie Bregman

Python: Lists vs. Tuples


DECEMBER 12, 2015 / BREGMAN / 0 COMMENTS

For Python beginners,the differences between lists and tuples might not be asclear
as to those who already got some experience using them.

Lets review the key differences between the two. hopefully, it will help you
choose the right type in your code and understand that although this post
called lists vs. tuples there is no real match here. each type has his own
use case.

Mutability
Mutability talks aboutthe capability of an objectto change or alter. This is one of the
noticeable difference between the two types.

First, lets use list:

1 >>> my_list = [1,2,3]


2 >>> my_list[2] = 9
3 >>> print my_list
4 [1, 2, 9]

Now lets try the same on tuple:

http://abregman.com/2015/12/12/python-lists-vs-tuples/ 1/7
12/10/2017 Python: Lists vs. Tuples Arie Bregman

1 >>> my_tuple = (1,2,3)


2 >>> my_tuple[2] = 9
3 Traceback (most recent call last):
4 File "<stdin>", line 1, in <module>
5 TypeError: 'tuple' object does not support item assignment

As you can see, List is mutable in Python, which means you can change it. You can
modify the current objects in a list, add new and remove them.

For tuple, all the mentioned above actions, will not work as a tuple is immutable. You
cant add or remove elements from a tuple.

Heterogeneous & Homogeneous


Tuples usually contain a heterogeneous sequence of elements, meaning the elements
are of differenttypes. Lists, on the other hand,contain homogeneous sequences
which mean only one data type for the whole list.

This is, in my opinion, an interesting distinction between the two,since its not explicit
asother and its more of a semantic difference. Eventually, you can use single type in
a tuple and different types in a list.

There are several analogiesused to emphasize this difference. My favoriteone is the


phone app. To save phone contacts, you will probably want to use a list, because your
contacts properties can be changed phone number, image, even name sometimes.

To save call logs you will want to use tuples. Call logs are used for viewing only. You
cant change call logs content. You dont want anyone to change the history details.
Only to watch them. (The onlyproblem with this examplemight be that youcan not
add objects to tuples and for call logs you would want to add additional calls. but lets
assume each time new call made, a new tuple being created )

Size
The size of tuple object in memory is smaller than list. but thats only noticeable when
using big objects.

1 >>> list = list(range(10000))


2 >>> tuple = tuple(range(10000))

http://abregman.com/2015/12/12/python-lists-vs-tuples/ 2/7
12/10/2017 Python: Lists vs. Tuples Arie Bregman

3 >>> print list.__sizeof__()


4 90088
5 >>> print tuple.__sizeof__()
6 80024

As you can see, the tuple is much smaller in size than list. That makes tuple operations
faster.

Dictionary identi er
You can use tuple as a dictionary identi er, but not list.

1 >>> li = [1,1]
2 >>> tu = (1,1)
3
4 >>> di = {li: 2}
5 TypeError: unhashable type: 'list'
6 >>> di = {tu: 2}
7 :)

Share this:

Related

Python: Objects comparison Python: Working with RPMs Ansible: write and run your
November 29, 2016 November 29, 2016 rst playbook
In "Python" In "Linux" December 25, 2015
In "Ansible"

Python

LIST PYTHON TUPLE

PREVIOUS POST NEXT POST

Python: Check whether a string is Palindrome Jenkins & Gerrit: trigger build on added
or not comment

http://abregman.com/2015/12/12/python-lists-vs-tuples/ 3/7
12/10/2017 Python: Lists vs. Tuples Arie Bregman

Leave a Reply
Your email address will not be published.

Name

Email

Website

Post Comment

Notify me of follow-up comments by email.

Notify me of new posts by email.

Search form SEARCH

RECENT POSTS
http://abregman.com/2015/12/12/python-lists-vs-tuples/ 4/7
12/10/2017 Python: Lists vs. Tuples Arie Bregman

Algorithms: Basic Exercises Part 1

Linear Programming: Graphical Method

jQuery Datatable Ajax: Change cells HTML after data is processed

Python: Working with Jinja2 templates

Ajax Datatable: changing row color based on the data

RECENT COMMENTS

Satya on Ansible: write and run your rst playbook

vamshi on Ansible: write and run your rst playbook

Viju on Ansible: write and run your rst playbook

Martin on Python: Objects comparison

Justin C. on Linux: Ulimit And Maximum Number Of Open Files

2017 ARIE BREGMAN UP

http://abregman.com/2015/12/12/python-lists-vs-tuples/ 5/7
12/10/2017 Python: Lists vs. Tuples Arie Bregman

http://abregman.com/2015/12/12/python-lists-vs-tuples/ 6/7
12/10/2017 Python: Lists vs. Tuples Arie Bregman

http://abregman.com/2015/12/12/python-lists-vs-tuples/ 7/7

S-ar putea să vă placă și