Python has quite a few built-in data structures.

  • Lists
  • Tuples
  • Dictionaries
  • Strings
  • Sets
  • Frozensets

Among these, I am going to start with lists.

A list keeps the order of objects stored in it. So does tuples and strings. Hence, these are also called sequence types in general.

A list is mutable – meaning we can add, delete or modify its elements.

>>> vehicle=['car','bus','truck','van']
>>> print(vehicle)
['car', 'bus', 'truck', 'van']

>>> print(vehicle[0])
>>> print(vehicle[3])

How to add a new element to an existing list ?

Use append method to add an element at the end

>>> vehicle.append('motorbike')
>>> print(vehicle)
 ['car', 'bus', 'truck', 'van', 'motorbike']

How to insert another list into my existing list ?

>>> vehicle=['car','bus','truck','van']
 >>> vessels=['ship','boat', 'yacht']
 >>> vehicle[1:1] = vessels
 >>> print(vehicle)
 ['car', 'ship', 'boat', 'yacht', 'bus', 'truck', 'van']

What if I want to add another list to my existing list at the end ?

I’m not allowed to insert into vehicle[4:4], because, index position 4 is not available. Hence, use the extend() method.

 >>> vehicle=['car','bus','truck','van']
 >>> vessels=['ship','boat', 'yacht']
 >>> vehicle.extend(vessels) //append vessels list at the end of vehicle list
 >>> print(vehicle)
 ['car', 'bus', 'truck', 'van', 'ship', 'boat', 'yacht']

How to replace a single element in the list ?

>>> vehicle=['car','bus','truck','van']
 >>> vehicle[1]='crewcab'
 >>> print(vehicle)
 ['car', 'crewcab', 'truck', 'van']

How to delete an element from the list ?

>>> vehicle=['car','bus','truck','van']
 >>> del vehicle[2]
 >>> print(vehicle)
 ['car', 'bus', 'van']

How to slice (subset) a list ?

>>> vehicle=vehicle=['car','bus','truck','van']
 >>> print(vehicle[1:3])
 ['bus', 'truck']

The slicing operation takes two parameter (optional). The first – 1 tells python to start from second element (index starts at 0). The second value – 3 means python has to end the slicing at 3rd element, but not include it in the slice.

Count number of occurrence of a given item in the list

>>> vehicle=['car','bus','truck','van', 'bus']
 >>> print(vehicle)
 ['car', 'bus', 'truck', 'van', 'bus']
 >>> vehicle.count('bus')

Find the index position of a given item in the list.

If the item appears more than once, index() method returns the first occurrence. If item is not present in the list, it raises an exception

>>> print(vehicle)
 ['car', 'bus', 'truck', 'van', 'bus']
 >>> vehicle.index('bus')
 >>> vehicle.index('van')
 >>> vehicle.index('motorbike')
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 ValueError: 'motorbike' is not in list

NOTE: index function has two optional parameters – two indices within which it should find the given item.

Sorting the list.

sort() function sorts the list in-place. So, after the sort operation, indices of items changes.

>>> print(vehicle)
 ['car', 'bus', 'truck', 'van', 'bus']
 >>> vehicle.sort()
 >>> print(vehicle)
 ['bus', 'bus', 'car', 'truck', 'van']

Reverse a list

>>> vehicle=['car','bus','truck','van', 'bus']
 >>> vehicle.reverse()
 >>> print(vehicle)
 ['bus', 'van', 'truck', 'bus', 'car']

Get the total number of items in the list

>>> len(vehicle)

How to traverse a list ?

I used the for loop.

When I start the for loop, it starts a new code block – that’s why there is the “:” at the end of the loop statement. Any statement that must be executed within the loop should be indented 4 spaces. Here there is only one print() statement.

Remember – Python takes only white spaces for indentation. Usually text editors insert a Tab instead of white spaces.

>>> for x in vehicle:
 ... print(x)

There is something called a list comprehension – a shorthand kind of syntax for creating and traversing lists. Though comprehensions are touted to be faster compared to the traditional for loop, I thought I will spend time on it later, as the syntax looks overwhelming.

Apart from all these, lists have built-in methods to use them directly as a stack or a queue.


Next I will learn about dictionaries.




Basic python packages for learning analytics

Written by Justin Jose

He is a post graduate in computers and has more than 14 years of industry experience with some of the leading Information Technology companies in India. Data-centric computing, ranging from data architecture to analytics is his area of interest.