Sunday, August 2, 2015

Python Basics - 010 - Lists and Tuples

Previous chapter: Operators in Python 3 Next Chapter: Functions



Lists and Tuples

Lets get one thing out of the way, Lists and Tuples are basically the same. Tuple is constant and immutable so the size or number elements in a tuple cannot be changed/increased.

A List is mutable, you can add or remove elements from the lists. Tuple is rarely used because it cannot be changed, but it is faster to create and iterate than a Python list.

using comma separated values enclosed with normal braces or without any braces will create a tuple, same values with square bracket will create a List. Lists are more similar to Arrays in Java than to LinkedLists.

See examples below, the index to access the elements start from 0, which is familiar to any programmer from c or java.


# ways to initiate a tuple
tupleExample = 5, 6, 2, 6
tupleExample1 = (5, 6, 7, 8)

print("Tuple ", tupleExample)
# accessing a tuple's element
print("Second element of tupleExample ", tupleExample[1])

# ways to create a list, use square brackets
listExample = [5, 2, 4, 1]

print("List:", listExample)
# accessing a List's element
print("Third element of list:", listExample[2])

Output will be:


Tuple   (5, 6, 2, 6)
Second element of tupleExample  6
List: [5, 2, 4, 1]
Third element of list: 4

Now that we've establishing that tuples are only static lists and we cannot modify it. Lists, however can be modified, Let's get to know how to modify a list and what all operations we can do.

We can get a part of the data out of the list by a process called slicing, we can slice a list by passing a range as a index value


# slicing of data is to get a subset of the list as another list, by providing a range for the index
print("a slice of the data:", listExample[1:4]) # brings up 1, 2 and 3rd element

We can get the elements from the last by using negative index values, see the example below to see how


# The last index value is -1 and the last but one is -2 so on and so forth
print("The last element in this list:", listExample[-1])

output:


The last element in this list: 1

We can use append to add a value to the end of a list. Here's an example:


listExample = [5, 2, 4, 1]
print("List:", listExample)
listExample.append(2)
print("2 added to the end of the List:", listExample)

output:


List: [5, 2, 4, 1]
2 added to the end of the List: [5, 2, 4, 1, 2]

As expected the value 2 is added to the end of the list, same can be done for a list of Strings or decimal values the same way it is done for integers.

What if we wanted to add an element at a specific position in the list, we could do that using the insert function of the list. Adding to the above example, here's a way of adding a value 77 at the third position


listExample.insert(2, 77)
print("77 inserted at 3rd location in the List:", listExample)

Let's see if the output reflects what we've tried to do and we can see below that we've got the result we wanted.


List: [5, 2, 4, 1, 2]
77 inserted at 3rd location in the List: [5, 2, 77, 4, 1, 2]

What if we added the 2 previously by mistake and wanna take it out of the list, we can use the remove function. remove function will remove the first occurring value as passed to the function. To remove a element based on position, we have to pass the list with the index as a parameter. Let's see the examples below to be more clear.


listExample.remove(2)  # will remove the first occurring 2 in the list
print("value 2 is removed from the list:", listExample)

listExample.remove(listExample[1])  # will remove the 2nd element of the list
print("2nd element removed from list:", listExample)

From the output below, we can see the expected behaviour


value 2 is removed from the list: [5, 77, 4, 1, 2]
2nd element removed from list: [5, 4, 1, 2]

The index can also be used in a different way to count from the last value, by passing a negative value to the index. Similar to how we access it.

To find the index of an element whose position is unknown, we can use the index function of the list object:


print("location of 5 in the list:", listExample.index(5))

To find out the number of occurrences of a particular value in a list, use the count() function of the list object.


print("no of times 2 occurs in the list:", listExample.count(2))

To sort the list, use the sort function. The sort function works for string lists as well where is sorts in alphabetical order.


listExample.sort()
print("Sorted List:", listExample)

output:


Sorted List: [1, 2, 4, 5]

Here's the complete example program for this chapter,


# ways to initiate a tuple
tupleExample = 5, 6, 2, 6
tupleExample1 = (5, 6, 7, 8)

print("Tuple  ", tupleExample)
# accessing a tuple's element
print("Second element of tupleExample ", tupleExample[1])

# ways to create a list, use square brackets
listExample = [5, 2, 4, 1]

print("List:", listExample)
# accessing a List's element
print("Third element of list:", listExample[2])

# slicing of data is to get a subset of the list as another list, by providing a range for the index
print("a slice of the data:", listExample[1:4])  # brings up 1, 2 and 3rd element

# we can access the list from the end, using negative index values.
# The last index value is -1 and the last but one is -2 so on and so forth
print("The last element in this list:", listExample[-1])
print("The last but second element in this list:", listExample[-2])


listExample = [5, 2, 4, 1]
print("List:", listExample)
listExample.append(2)
print("2 added to the end of the List:", listExample)

listExample.insert(2, 77)
print("77 inserted at 3rd location in the List:", listExample)

listExample.remove(2)  # will remove the first occurring 2 in the list
print("value 2 is removed from the list:", listExample)

listExample.remove(listExample[1])  # will remove the 2nd element of the list
print("2nd element removed from list:", listExample)

# to find the index value of the element 5, it gives the first matching values's index
print("location of 5 in the list:", listExample.index(5))

# to find out the number of occurrences of a particular value
print("no of times 2 occurs in the list:", listExample.count(2))

# To sort the list, use the sort function
listExample.sort()
print("Sorted List:", listExample)

For verification, here's the output of the program


Tuple   (5, 6, 2, 6)
Second element of tupleExample  6
List: [5, 2, 4, 1]
Third element of list: 4
a slice of the data: [2, 4, 1]
The last element in this list: 1
The last but second element in this list: 4
List: [5, 2, 4, 1]
2 added to the end of the List: [5, 2, 4, 1, 2]
77 inserted at 3rd location in the List: [5, 2, 77, 4, 1, 2]
value 2 is removed from the list: [5, 77, 4, 1, 2]
2nd element removed from list: [5, 4, 1, 2]
location of 5 in the list: 0
no of times 2 occurs in the list: 1
Sorted List: [1, 2, 4, 5]

Lists are very important to any programming language, just as important as string manipulations. Let's move on to another integral part of any programming language: functions.


P.S: all the code used in this tutorial is available in github


References:

  1. https://docs.python.org/3.4/tutorial/introduction.html#lists



Previous chapter: Operators in Python 3 Next Chapter: Functions

No comments:

Post a Comment