The True difference between List, Tuples, Sets, Dictionary : Python Data Structures for Data Science
As a student i always had a confusion on all these 4 pillars. Understanding when and how to use will obviously come through practice whereas there are several things which you can keep in your mind which will really help in choosing these data structures efficiently. Reading it for the first time may look easy and yes its easy, when you dive deeper into it and know why u use these data structures of python.
The number of people using python is increasing tremendously. Knowing the fact python is not for all types of problems but yeah u can solve using it pretty easily. So lets see what are the major difference in these data structures.
LISTS
Lists is an sequence of items or data / collection of items
Lists are mutable (Mutable means — can be changed)
All the items in the Lists can be changed (Edited/Modified).
#Create a List
a = []
Yesss your list was created!!.
As Lists are mutable there can be various operations on list.
#Extend or appenda.append('a')
#this will append to last of the list#Insert to lista.insert(2,'abc')
# in the above syntax 2 is the index and then the value#Remove
a.remove(2)
#Remove from location/index 2
Tuples
These are same as lists but with one twist
Tuples are immutable!
Immutable — Cannot be changed
Yes that’s a real twist.
#create tuple
a = ()#creating a complex tuple
a = (1,(1,3,4),[1,'abc',22],34)
# Tuple inside a tuple as well as lists inside tuple#Rest everything is same as list
#Only thing is you cant edit/modify a tuple
Sets
Sets are unordered
These cannot be indexed like other data structures.
So, Index based accessing the items won’t work.
sets are Mutable.
And one ore added advantage is sets doesn’t allow Duplicates.
#create a empty set
s = {}#With elements
s = {1,2,3,4,5}#Make set from list
s = set{ [1,2,3] }#using index we cannot access them because they are Unordered
#So u can add using this
s.add(10)#add list to set
s.update([4,2,51])#remove element
s.discard(item)
or s.remove(item)s.pop() #remove items randomly
s.clear() #remove all items of set
Dictionary
Dictionary are unordered collection of items.(so no indexing)
Hash Table and dictionary are similar.
Dictionary are Mutable.
#create a new dictionary
my_dict = {}my_dict = {1:'a',2:'b'}#create a dict with list of tuples
my_dict = dict([(1,'abc'),(2,'xyz')])
Dictionary are key:value pairs
#Modify or edit
my_dict['key'] = 'new Value'#delete dict
my_dict.pop('key')
There are various operations on all these 4 Pillars!!
Read through many posts and gain knowledge to be a proficient user of all these data structures.
There is no end for learning!!