List vs tuple vs dictionary in Python

PythonServer Side ProgrammingProgramming

List and Tuple objects are sequences. A dictionary is a hash table of key-value pairs. List and tuple is an ordered collection of items. Dictionary is unordered collection.

List and dictionary objects are mutable i.e. it is possible to add new item or delete and item from it. Tuple is an immutable object. Addition or deletion operations are not possible on tuple object.

Each of them is a collection of comma-separated items. List items are enclosed in square brackets [], tuple items in round brackets or parentheses (), and dictionary items in curly brackets {}

>>> L1=[12, "Ravi", "B.Com FY", 78.50] #list
>>> T1=(12, "Ravi", "B.Com FY", 78.50)#tuple
>>> D1={"Rollno":12, "class":" FY", "precentage":78.50}#dictionary

List and tuple items are indexed. Slice operator allows item of certain index to be accessed

>>> print (L1[2])
B.Com FY
>>> print (T1[2])
B.Com FY

Items in dictionary are not indexed. Value associated with a certain key is obtained by putting in square bracket. The get() method of dictionary also returns associated value.

>>> print (D1['class']) FY
>>> print (D1.get('class')) FY

Published on 09-Feb-2018 00:34:00