When should one use dictionaries and when should one use
lists in Python
=====================================================
* Lists are the most widely used data when the data is stored in
the form of integral indexes with
some starting position to some given
position within the Python list
* Lists are also mostly used when the data that needs to be maintained
in a certain specific manner or where the data needs to be sorted where finding some data is only good enough when the user of the
given set of code is knowing the exact index or position of the item element where the list item needs to be searched or retrieved
* Lists are also used when the ordering of items within the
List item object is irrelevant as any given element within a list can be searched and retrieved just by using some conditional loops which will iterate over the list item object
and then the item could be put to use directly without any form of replacement or elimination within the data
structure
* One of the main highlighting important points about Lists as a data structure for item
storage is that Lists can be
used for storage of other lists and tuples as a data storage within it because of which the main list containing the sub-lists within it could act as a parent list
object which can keep within it other child objects for storage and retrieval
as well
* Now coming to dictionaries and what they are
used for :-
* Dictionaries are perfect storage objects when the data is to be stored on the basis of positonal
indexes coupled with an adjoining value associated for that index position . Here also the ordering of the data is not of great matter and does not need to sort
the stored data . Finding any associated data of the dictionary object needs only a key value of that object
* Example where a List
is used for data storage is an
example of a shopping cart where any stored item
object's storage does not matter .
* Example where a Dictionary Item object is used as a storage
object is that of a an examination roll / rank number sheet ( parikhya patra ) where people are arranged on
the basis of their marks obtained in some examination
from Rank 1 to the last rank in the
exam . Usually the person who obtains the highest marks in a given exam is attributted the 1st rank and the first roll number for that examination is also alloted to the first rank holder after the
results are out , the successive role numbers are also attributed according
the net marks obtained by an examiner in the considered case of
the examination till the last
rank/roll number of the examiner .
* A dictionary is very useful
data storage object where one may need to
access a given data based on the item
that is present over a given index position within the storage object of the
data structure .
* As a measure of how important the dictionaries are - every module , class , function and method written in Python in the form of dictionaries where all the attribute names are in the form of Keys and all the attribute values there in
can be found quickly .
* Therefore when one wants
to decide upon which data structure to be
used for a certain type of data object one might need to consider
the best use cases of both
these data structure objects : Lists and Dictionary Items .
======================================
To sum up once again for Lists and Dictionaries
=====================================
* Lists are ordered data structure objects that means that the program has control over the
order in which the data is stored . This data is also sortable that means that the program can sort the data into whatever order when the data is needed to be fetched and retrieved , the list is also containing a list of iterable items i,e items that can be accessed one item at a time when the list object is iterated or
executed within a loop .
* Dictionaries are like the partially ordered data structures that
can control the order or the manner in which the data items are inserted and stored within the
object in the form of keys and attributes . Dictionaries are non-sortable
and iterable in pairs of both keys
and associated value objects .
Last
modified: 18:49
No comments:
Post a Comment