Python Collection Types


Here we discuss about three main collection types in Python.

Python Lists


Python Lists are collections which are ordered and changeable(Mutable), defined between square brackets - [ ]

Acessing Elements


We can access elements of a list as follows. Remember that the first item has index 0.

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


We can access a range of elements using the starting and ending index values. The output will be another list.

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


We can change certain elemets in a list, add new elements and remove elements of 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


len( ) - keyword is used to get the number of elements in a list(length).

Looping through a list

We can use both while & for loops.

You can learn about Python loops here.



Python Tuple


Python Tuples are collections which are ordered and unchangeable(Immutable), defined between parenthesis - ( )

Acessing Elements


We can access elements of a tuple as follows. Remember that the first item has index 0.

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


We can access a range of elements using the starting and ending index values. The output will be another tuple.

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


Tuples are Immutable. Which means we can't change, add or remove element from 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


len( ) - keyword is used to get the number of elements in a tuple(length).

Looping through a tuple


We can use both while & for loops.

You can learn about Python loops here.


List Vs. Tuple


Although tuples are immutable(unchangeable), we can overcome this problem since we can covert list to tuple and even tuple to list.

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)



Python Dictionaries are collections which are ordered and changeable(Mutable) with unique keys and respective values, defined between curly brackets - { }

Structure

myDictionary = {key1:value1, key2:value2,...}

Acessing Elements


We can access values of a dictionary as follows.

Change a Dictionary


We can change values in a dictionary, add new elements and remove elements from 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.



Length of a Dictionary


len( ) - keyword is used to get the number of elements in a dictionary(length).

Looping through a Dictionary


We can access values & keys with for loop.

Looping through keys.


Looping through values.

You can learn about Python loops here.






Concept & Design by Code94 Labs