Python - Lists Vs PDF
Python - Lists Vs PDF
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.
http://abregman.com/2015/12/12/python-lists-vs-tuples/ 1/7
12/10/2017 Python: Lists vs. Tuples Arie Bregman
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.
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.
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.
http://abregman.com/2015/12/12/python-lists-vs-tuples/ 2/7
12/10/2017 Python: Lists vs. Tuples Arie Bregman
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
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
Website
Post Comment
RECENT POSTS
http://abregman.com/2015/12/12/python-lists-vs-tuples/ 4/7
12/10/2017 Python: Lists vs. Tuples Arie Bregman
RECENT COMMENTS
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