Common properties of list , tuple , set and dictionary Python

Here let us see some of the properties similar in between list , tuple ,set and dictionary.Some properties may be common to both list and tuple while some other properties may be similar between set and dictionary.Knowing their properties thoroughly will help you to make decision better on which data type to use over which and in what situation.

i)Ordered or unordered

Among the four data type only list and tuple are ordered.Meaning that storing elements in list and tuple follows certain ordering.For instance,in both set and list all the elements placed are given a certain position,the first element is given the 1st position or index value 0,and the second element is given index 1 and the third element the index value 2 and so.So if you know the position of the element we can easily access the element using the index value.

The set and dictionary on the other hand are unordered.The elements are not stored in a specific ordered or they are not given any index value to specify their position.In dictionary you access the element using the key.Also note set does not allow duplicate element.

>>> ls=[23 , 45 , 'New'] #List
>>> tp=(34 , 'Text' , '567') #Tuple
>>> set ={ 34 , 78 , 78 , 90} #Set
>>> dic={ 'key1':'New' , 'key2':'Text' , 'key3':'*9394' }
>>> ls[2]
'New'
>>> tp[1]
'Text'
>>> dic['key3']
'*9394'
>>> set
{34, 90, 78}

If you want order data type in your program choose between list or tuple.But list is more flexible so I will go with list.

Know more about list :Python list

If your program requires storing or manipulating data and if each of the data holds a unique property then you can go with dictionary and make the property as the key of the data.

Know more about dictionary :Python dictionary


Performance

By performance we mean how much time each data type will take to perform various operations.The operations can range from insertion or deletion to searching or rearranging data from the object.

For instance it is said that operation like insertion can take O(n) in list,while they are
O(1) for a dictionary.

To know what does O(1) and O(n) mean Google ‘The big O notation‘.Wikipedia has a huge page describing about it.


Slicing

By slicing we mean taking a part of elements from object from any position.Slicing is done for either assignment or for modification of the elements.Note slicing is allowed only in list and tuple because they are ordered.This means set and dictionary has no privilege of slicing.

Let us perform a simple slicing in list object.Here we will take the elements between the index number 3 to 6.For tuple object we will slicing the element from 5 to the end.

>>> ls=[23 , 45 ,89 , 90 ,'new' , 'text' , '40494' , 'Textual' , ['Graphicx'] ]
>>> ls1=ls[3:7] #slicing from 3 to 6th index
>>> ls1
[90, 'new', 'text', '40494']
>>> #Tuple example
>>> tp=(23 , '67,56' , 'tue' , 'Mon' , 'Wed' , 'thurs' , 'fri' , 'Sat' , 'Sun' )
>>> tp1=tp[5:]
>>> tp1
('thurs', 'fri', 'Sat', 'Sun')

More example in slicing is provided with string discussion,you can visit he link .Note the concept applied in string can be also applied with list and tuple.



Leave a Reply

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