Python Collection Types
Here we discuss about three main collection types in Python.
Python Lists
Acessing Elements
In the above example we can identify that we again use square brackets - [ ] to access elements of a list.
~And also we can declare a list inside a list~
Range of Elements
Below code will access the index 1 to index 5 (not incude).
In range function it will not reach the end index. Therefore to access 1 to 4 index elements the code is as above.
Change a List
.append - keyword is used when adding elements.
.remove - keyword is used when removing elements elements.
We can combine two or more lists using concatenation.
Length of a List
Looping through a list
Python Tuple
Acessing Elements
In the above example we can identify that we use square brackets - [ ] to access elements of a list.
~And also we can declare a tuple inside a tuple~
Range of Elements
Below code will access the index 1 to index 5 (not incude).
In range function it will not reach the end index. Therefore to access 1 to 4 index elements the code is as above.
Change a Tuple
The following codes will throw an error since tuple are Immutable(unchangeable).
But, we can combine two or more tuples using concatenation.
Length of a Tuple
Looping through a tuple
List Vs. Tuple
In the above situation we have converted a 'tuple' into a 'list' and added a new element and converted the 'list' back to a 'tuple'.
Python Dictionary (dict)
Structure
myDictionary = {key1:value1, key2:value2,...}
Acessing Elements
Change a Dictionary
Changing an exisiting value in a dictionary.
Adding a new element to a dictionary.
del - keyword is used when deleting an elemenet.
We can't combine two or more lists using concatenation. It'll throw an error.