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 - [ ]
myList = ["Hello","World",100,50.5] print (myList)
Press Run to execute.

Acessing Elements


We can access elements of a list as follows. Remember that the first item has index 0.
myList = ["Hello",100,["Another","List"],"World"] print (myList[0]) print(myList[1]) print(myList[3][0]) print(myList[-1]) print(myList[2][1])
Press Run to execute.

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).

names = ["Anji","Paul","Hafsa","John","Hlasa","Chuan"] print(names[1:5])
Press Run to execute.

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.

names = ["Anji","Paul","Hafsa","John"] #Change the first element names[0] = "Alice" print(names) #Add a new element names.append("Johnny") print(names)
Press Run to execute.

.remove - keyword is used when removing elements elements.

names = ["Anji","Paul","Hafsa","John"] names.remove("Anji") print(names)
Press Run to execute.

We can combine two or more lists using concatenation.

names1 = ["Anji","Paul"] names2 = ["Hafsa","John"] names = names1 + names2 print(names)
Press Run to execute.

Length of a List


len( ) - keyword is used to get the number of elements in a list(length).
names = ["Anji","Paul","Hafsa","John","Hlasa"] print (len(names))
Press Run to execute.

Looping through a list

We can use both while & for loops.
names = ["Anji","Paul","Hafsa","John","Hlasa"] for i in names: print(i)
Press Run to execute.
names = ["Anji","Paul","Hafsa","John","Hlasa"] index = 0 while (index < len(names)): print(names[index]) index = index + 1
Press Run to execute.

You can learn about Python loops here.



Python Tuple


Python Tuples are collections which are ordered and unchangeable(Immutable), defined between parenthesis - ( )
myTuple = ("Hello","World",100,50.5) print (myTuple)
Press Run to execute.

Acessing Elements


We can access elements of a tuple as follows. Remember that the first item has index 0.
myTuple = ("Hello",100,("Another","tuple"),"World") print (myTuple[0]) print(myTuple[1]) print(myTuple[3][0]) print(myTuple[-1]) print(myTuple[2][1])
Press Run to execute.

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).

names = ("Anji","Paul","Hafsa","John","Hlasa","Chuan") print(names[1:5])
Press Run to execute.

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).

names = ("Anji","Paul","Hafsa","John") #Change the first element names[0] = "Alice" print(names)
Press Run to execute.
names = ("Anji","Paul","Hafsa","John") #remove the first element names.remove("Anji") print(names)
Press Run to execute.

But, we can combine two or more tuples using concatenation.

names1 = ("Anji","Paul") names2 = ("Hafsa","John") names = names1 + names2 print(names)
Press Run to execute.

Length of a Tuple


len( ) - keyword is used to get the number of elements in a tuple(length).
names = ("Anji","Paul","Hafsa","John","Hlasa") print (len(names))
Press Run to execute.

Looping through a tuple


We can use both while & for loops.
names = ("Anji","Paul","Hafsa","John","Hlasa") for i in names: print(i)
Press Run to execute.
names = ("Anji","Paul","Hafsa","John","Hlasa") index = 0 while (index < len(names)): print(names[index]) index = index + 1
Press Run to execute.

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.
myTuple = ("Hello","World",100,50.5) print(myTuple) #convert tuple to a list myTuple = list(myTuple) print (myTuple) #Add a new element myTuple.append("New Element") #convert list to a tuple myTuple = tuple(myTuple) print(myTuple)
Press Run to execute.

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,...}
myDictionary = {'first':'gold','second':'silver','third':'bronze'} print(myDictionary)
Press Run to execute.

Acessing Elements


We can access values of a dictionary as follows.
myDictionary = {'first':'gold','second':'silver','third':'bronze'} print(myDictionary['first'])
Press Run to execute.

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.

myDictionary = {'first':'gold','second':'silver','third':'bronze'} myDictionary ['first'] = 'platinum' print(myDictionary)
Press Run to execute.

Adding a new element to a dictionary.

myDictionary = {'first':'gold','second':'silver','third':'bronze'} myDictionary ['fourth'] = 'bronze' print(myDictionary)
Press Run to execute.

del - keyword is used when deleting an elemenet.

myDictionary = {'first':'gold','second':'silver','third':'bronze','fourth':'bronze'} del myDictionary ['fourth'] print(myDictionary)
Press Run to execute.

We can't combine two or more lists using concatenation. It'll throw an error.

name1 = {'first':'Donald'} name2 = {'second':'Trump'} name = name1 + name2 print(name)
Press Run to execute.


Length of a Dictionary


len( ) - keyword is used to get the number of elements in a dictionary(length).
myDictionary = {'first':'gold','second':'silver','third':'bronze'} print (len(myDictionary))
Press Run to execute.

Looping through a Dictionary


We can access values & keys with for loop.

Looping through keys.

myDictionary = {'first':'gold','second':'silver','third':'bronze'} for key in myDictionary: print(key)
Press Run to execute.

Looping through values.

myDictionary = {'first':'gold','second':'silver','third':'bronze'} for key in myDictionary: print(myDictionary[key])
Press Run to execute.

You can learn about Python loops here.






Concept & Design by Code94 Labs