- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the differences and similarities between tuples and lists in Python?
Both List and Tuple are called as sequence data types of Python. Objects of both types are comma separated collection of items not necessarily of same type.
Similarities
Concatenation, repetition, indexing and slicing can be done on objects of both types
>>> #list operations >>> L1=[1,2,3] >>> L2=[4,5,6] >>> #concatenation >>> L3=L1+L2 >>> L3 [1, 2, 3, 4, 5, 6] >>> #repetition >>> L1*3 [1, 2, 3, 1, 2, 3, 1, 2, 3] >>> #indexing >>> L3[4] 5 >>> #slicing >>> L3[2:4] [3, 4]
>>> #tuple operations >>> T1=(1,2,3) >>> T2=(4,5,6) >>> #concatenation >>> T3=T1+T2 >>> T3 (1, 2, 3, 4, 5, 6) >>> #repetition >>> T1*3 (1, 2, 3, 1, 2, 3, 1, 2, 3) >>> #indexing >>> T3[4] 5 >>> #slicing >>> T3[2:4] (3, 4)
Following built-in functions are common to both types
len() − return number of elements in sequence
>>> L1=[45,32,16,72,24] >>> len(L1) 5 >>> T1=(45,32,16,72,24) >>> len(T3)
max() − returns element with largest value.
>>> max(L1) 72 >>> max(T1) 72
min() − returns element with smallest value.
>>> max(T1) 72 >>> min(L1) 16 >>> min(T1) 16
Differences
List object is mutable. Hence, it is possible to append, update or delete an item from list.
>>> L1=[45,32,16,72,24] >>> L1.append(56) >>> L1 [45, 32, 16, 72, 24, 56] >>> L1.insert(4,10) #insert 10 at 4th index >>> L1 [45, 32, 16, 72, 10, 24, 56] >>> L1.remove(16) >>> L1 [45, 32, 72, 10, 24, 56] >>> L1[2]=100 #update >>> L1 [45, 32, 100, 10, 24, 56]
Tuple is immutable object. Any operation that try to modify it , results in AttributeError
T1.append(56) AttributeError: 'tuple' object has no attribute 'append' >>> T1.remove(16) AttributeError: 'tuple' object has no attribute 'remove' >>> T1[2]=100 TypeError: 'tuple' object does not support item assignment
- Related Articles
- What are the similarities and differences between MySQL ORD() and ASCII() functions?
- What's the difference between lists and tuples in Python?
- How do I convert between tuples and lists in Python?
- List the similarities and differences between aerobic and anaerobic respiration.
- What are the similarities and difference between Hinduism and Buddhism?
- What are the similarities between iron, copper, and aluminium?
- State similarities and differences between the laboratory thermometer and the clinical thermometer.
- What are the differences between json and simplejson Python modules?
- What are the differences between readline() and readlines() in Selenium with python?
- What are the differences between xpath and css in Selenium with python?
- Why does Python allow commas at the end of lists and tuples?
- What are the differences in between python 2.x and python 3.x versions?
- What are the key differences between Python 2.7.x and Python 3.x?
- Difference between List and Tuples in Python.
- What are the differences between list, sequence and slice data types in Python?

Advertisements