List and Tuples in Python

List is widely used data structure in Python. List is designed to keep multiple values in one order so you can access them in sequence. Values in a list called items and each item has its own address. As you have seen in Strings, string is also a list of character and each character has its own address.

Creating a list in Python is very easy, start declaring a list by open bracket “[“, add comma “,” separated items and close this with closing bracket “]”. Check following example,

[10, 20, 30, 40, 50, 60]

You have successfully declared a list. List can be assigned to a variable so you can access the list items.

## integer list
myIntegerList = [10, 20, 30, 40, 50, 60]

## print complete list

## get size of list

Now you have a list type variable called myIntegerList. You can print a complete list and can get size of a list by using len function.

List Indexing

Before moving forward, lets have a quick look on list architecture. When a List is stored in memory, every single item is assigned an index number and this index starts from 0.

0 1 2 3 4 5
10 20 30 40 50 60

Each and every item in this list can be accessed by it’s index. Item 10 has index 0, item 20 has index 1 and  60 can be accessed by index 5. To access the specific value in list use [ ], for example myIntegerList [0] will return item 10 from index 0.

print(myIntegerList[0]) # First element
print(myIntegerList[1]) # Second element

Python not only assign 0 based indexing to the list but also assign negative indexing as well and this index starts from last item of the list with -1. The image of negative indexing is as follow:

-6 -5 -4 -3 -2 -1
10 20 30 40 50 60
print(myIntegerList[-1]) # last element
print(myIntegerList[-2]) # second last element

Different types of List

Python provides two types of lists. One is homogeneous list which contains one type of elements and second is inhomogeneous list which can have any type of items. 

myStringList = ["one","two","three","four","five","six"]

print(myStringList[0]) # First element
print(myStringList[1]) # Second element

print(myStringList[-1]) # last element
print(myStringList[-2]) # second last element

myIntegerList and myStringList are homogeneous because they have one type of elements but myList is inhomogeneous 

myList = [1,"two",2.2,"three",3.4,"four",5.5,"six"]

print(myList[0]) # First element
print(myList[1]) # Second element

print(myList[-1]) # last element
print(myList[-2]) # second last element

because they have different types of elements including string, integer and float. Python is very flexible, most of the time it would let you do what you can wish.

crazyList = [2,"two",3+5+8,9/4]

List and its functions

Python provides useful functions which can be used to achieve different tasks. You can get the complete list of functions by using following command.

myhealthylist = ["Apple", "Banana", "Orange", "Lemon"]
dir(myhealthylist) # complete list of functions

As you know that 2 strings can be combined with “+” sign

apple = "Apple "
banana = "Banana"
fruit = apple + banana

You can do the same with lists, combine two lists with “+” sign

lista = [2,5,876,-12,0]
listb = [11,22,33,44,55]

listc = lista + listb

You can append new value in list by using append function.

myhealthylist = ["Apple", "Banana", "Orange", "Lemon"]


You can count occurrence of one item in list by using count function.

print("appearance of Banana is ", myhealthylist.count("Banana"), " time(s)")

In other programming languages, pop function removes only top most value of list but Python allow you to pop out any value from any location. To remove any value from list, provide value index to pop function. 

print("Remove first value")

print("Remove 3rd value")

print("Remove value from end")

Tuples in Python

Tuples and Lists are twin sisters, both data structures are lists types and they are essentially identical in behaviour but they are bit different when it comes to changing their values.

List can be declared with “[ ]” brackets but Tuples are declared with “( )” small brackets.

## integer list
myList = [10, 20, 30, 40, 50, 60]

## integer tuple
myTuple = (10, 20, 30, 40, 50, 60)

You can append or add more items in list after declaration but you cannot do it in tuples, once the value is assigned at declaration time, these values are last and final. 

## integer list
myList = [10, 20, 30, 40, 50, 60]

## integer tuple
myTuple = (10, 20, 30, 40, 50, 60)
## following line will throw an error that this append attribute is not available in tuple

You can update or replace any value in list but you cannot do that in tuples

## integer list
myList = [10, 20, 30, 40, 50, 60]
myList[0] = 70

## integer tuple
myTuple = (10, 20, 30, 40, 50, 60)
## following line will throw an error that item assignment is not supported.
myTuple[0] = 70

Mutable and Immutable 

Lists are mutable or changeable but tuples are immutable or cannot be updated. Let deep dive and see how tuples are used in Python. As you know and remember that items within “( )” brackets are tuples. Open Python shell and type following lines

t = 2,3,4


As you can see that the output of print statement is (2,3,4) and type of t is tuple. Lets try another example, create an add function with one parameter and return value with addition. add function will return (5, 10, 15) and its tuple. 

Convert from list to tuple and tuple to list

Now you know that tuples are immutable and cannot be updated, but there is one way to use or update them. Pass tuple to the list function and it will return list. 

## value is a tuple type 
value = (5,6,7)


## convert from tuple to list
value_as_list = list(value)


Now you can update the value_as_list, Once you are done with your changes you can easily convert this list back to tuple by using tuple function

value = tuple(value_as_list)

Always use tuples when list of values are static and does not require update, for example days of week.

days_of_week = ('Sat', 'Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri')