In this post, we will see collections in Python.
Python do not have native array, instead it provides different collection classes to handle data efficiently.
List
A list in Python is an ordered collection of data. We can store any type of data in a list unlike array that contains only one type of data.
Syntax:
list_name = [] list_name = [comma separated values]
Example:
list_1 = [10,20,30,40,50] list_2 = ["First", "Second", "Third", "Fourth"] list_3 = ["India", "New Delhi", 1947]
Accessing List Elements
We can access list elements using index values. For example:
list_1[0] => First element list_1[1] => Second element list_1[len(list_1)] => Last element # len() returns number of elements in list list_1[-2] => Second element from the right
Looping a list
Here is an example that uses for loop to read list elements using index:
numlist = [5,4,7,6,8,3] size = len(numlist) for i in range(size): print(numlist[i])
To access list without index, we can use:
numlist = [5,4,7,6,8,3] for x in numlist: print(x)
Extracting list values
We can extract elements from a list like this:
The following example extracts elements from index 1 to index 4 (5 is not included)
numlist = [5,4,7,6,8,3] => [4, 7, 6, 8] sublist = numlist[1:5]
The following example extracts all elements from index 1 to end
numlist = [5,4,7,6,8,3] => [4, 7, 6, 8, 3] sublist = numlist[1:]
The following example extracts all elements up to the index 2 ( 3 not included )
numlist = [5,4,7,6,8,3] => [5,4,7] sublist = numlist[:3]
Adding elements
To add element at the end of the list, we can write:
numlist = [5,4,7,6,8,3] numlist.append(9) => [5,4,7,6,8,3,9]
Inserting elements in list
To insert an element in a list element by index, we can write:
numlist = [5,4,7,6,8,3] numlist.insert(1,2) => [5, 2, 4, 7, 6, 8, 3]
Deleting list elements
To remove list element by index, we can write:
numlist = [5,4,7,6,8,3] del numlist[2] => [5,4,6,8,3] # removes element at index numlist.remove(7) => [5,4,6,8,3] # removes given element numlist.pop() => [5,4,6,8,3] # removes last element
Initializing list with default calculated values
Here is an example that initializes a list with default calculated values:
myList=[i*i for i in range(5)] => [0,1,4,9,16] print(myList)
Concatenating lists
We can concatenate two lists like this:
numlist_1 = [1,2,3] numlist_2 = [4,5,6] numlist_3 = numlist_1 + numlist_2 => [1,2,3,4,5,6]
Sorting and reversing list
We can concatenate two lists like this:
numlist = [5,4,7,6,8,3] numlist.reverse() => [3,8,6,7,4,5] numlist.sort() => [3,4,5,6,7,8]
Tuple
A tuple is very similar to list with one difference, it is immutable i.e. its values cannot be changed once added.
Syntax:
tuple_name = ()
tuple_name = (comma separated values)
Example:
tup = (4,5,6,7,8)
Rest is similar to list.
Dictionary
A dictionary is a key-value storage data type. Each value in the dictionary has a unique corresponding key.
Syntax:
dictionary_name = {} dictionary_name = dict() dictionary_name = { 'key1':'value1', 'key2':'value2' } dictionary_name = dict( key1 = value1, key2 = value2 )
Example:
dict_country = {'India':'New Delhi', 'Japan':'Tokyo'} print(dict_country['India']) => New Delhi
Another way of working with dictionary is:
dict_country = dict( India='New Delhi', Japan='Tokyo') print(dict_country['India']) => New Delhi
Updating dictionary values
To update existing values in dictionary, we write:
dict = {1:10, 2:20, 3:30} dict[1] = 100
Deleting dictionary values
To delete a value in a dictionary, we write:
del dict[1] # delete entry with key 1 del dict # delete entire dictionary dict.clear() # remove all entries
Looping a dictionary
dc = {1:10, 2:20, 3:30}
To loop through dictionary values, we write:
keys = dc.keys() for key in keys: print(key)
for key in keys: print(key , " => " , dc[key])
values = dc.values() for value in values: print(value)
for key,value in dc.items(): print(key , ' ' , value)
Extending dictionary
We can add the data of one dictionary to another. Here is an example:
dict_countries = dict( India='New Delhi', Japan='Tokyo') dict_more_countries = dict( SriLanka='Colombo', Russia='Moscow', **dict_countries) print(dict_more_countries)
**dict_countries refers to existing dictionary to be added
Another way of doing the same is:
dict_countries = {'India':'New Delhi', 'Japan':'Tokyo'} dict_more_countries = {'SriLanka':'Colombo', 'Russia':'Moscow'} dict_more_countries.update(dict_countries) print(dict_more_countries)