What is a list?
You may think lists as a group of variables. We may use 10 different variables called x1,x2,…x10 or we may use one list with 10 items in it.
Lists are similar to arrays in some other languages.
Notation is x = [a,b,c,d,e]
The elements can be values or variables containing values, or mix of those. Variables will be changed with values.
The elements can be any type, types can be mixed.
x = [1,2,3,4,5]
a,b,c = 1,2,3
x = [a,b,c]; y = [a,1,b,2,c,3]
Accessing list elements
The lists elements have indexes starting from 0, and incrementing by 1.
[1,2,3,4,5] list has indexes of 0,1,2,3,4.
We can access a list element by x[i] notation, where i is the index.
x=[1,2,3,4,5]; y=['a','b','c','d','e'];
x[0] #prints 1
y[3] #prints d
Also we may use negative indexes, these start from -1 for the rightmost element, decrease by one up to the leftmost element.
[a,b,c,d,e] list has indexes of both 0 1 2 3 4 and -5 -4 -3 -2 -1.
If we go out of index, python gives an error.
x=[1,2,3,4,5]; y=['a','b','c','d','e'];
x[-2] #prints 4
y[-4] #prints b
Slicing
We may access some part of the list by slicing operator :
It has two numbers a:b showing index numbers
The sub-list starts from a(inclusive), ends at b (exclusive)
The resulting value is always a list, even with one element, or empty
If we go out of index, or have larger index on the left, python doesn’t give an error
x=[1,2,3,4,5]
print(x[2:4], x[2:3], x[3:2], x[-2:-10])
If we skip left or right part, python replaces with first index / last index +1 automatically
x=[1,2,3,4,5]
x[2:] #same as [2:5], last index+1 is 4+1
x[:2] #same as [0:2], first index 0
x[:] #same as [0:5], last index+1 is 4+1
Alternative to last index value:
We may use len(x) instead of last index value
These are all same:
x=[1,2,3,4,5]
print( x[2:5], x[2:], x[2:len(x)] )
Reverse slicing, skipping while slicing
Default slicing behavior is starting from left and counting 1 to the right.
We may change this behavior, by stating the count number.
- count for left tracing.
x=list(range(10)) #same as x=[0,1....9]
x[2:10:3] #start from 2 up to 9, count by 3 (to the right)
x[10:2:-3] #start from 10 up to 3, count by -3 (to the left)
We use -1 for reverse order without skipping
To reverse the list, just type x[::-1]
All slicing operations creates a new list, doesn’t change the original
x=list(range(10))
x[::-1]
To copy the list, just type x[:]
x=list(range(10)); id(x)
y=x[:]; id(y)
Combining lists
We may add two lists together with + or .extend() method.
+ doesn’t change the list, creates a new one.
.extend() changes the list and returns it.
.extend() also takes other list like multi variable data structures, which will be seen later.
x=list(range(5)) #0...4
y=list(range(5,10)) #5...9
z=x+y #0...9
x.extend(y) #0...9 same as x=x+y or x+=y
y.extend(x) #5...9 0...4
Modifying and adding items to lists
We can modify the lists by accessing elements and then treating them like regular variables.
x=list(range(10)); print(x)
x[5]=45; x[7]=33; print(x)
We can not add an item to the list by using an out of range assignment
x[10]=22 doesn’t work since max index is 9.
We use .append() method for that.
x=list(range(5)); print(x)
x.append(5); print(x)
We can also use .insert() method to insert to a specific index, not just at the end of the list.
<list>.insert(<index>, <item>)
x=list(range(5)); print(x)
x.insert(2,5); print(x)
Deleting items from list
We may have several tools to delete items from the list.
del <list>[<index>] #deletes from specific index
<list>.pop(<index>] #deletes from specific index. also returns the value
#defaults to last item
<list>.remove(<item>) #removes the first occurrence of the item
<list>.clear() #clears the whole list, empty list remains
del <list>[:] #clears the whole list, empty list remains
del <list> #deletes the list, nothing remains
x = list(range(10)); print(x)
del x[3]; print(x) #removes index 3
x.pop(); print(x) #removes last item, index -1
x.pop(6); print(x) #removes index 6
x.remove(5); print(x) #removes item value 5
x.remove(5); print(x) #no more item value 5 left, gives error
x.clear(); print(x) #removes all, now the list is empty
x = list(range(10)); print(x)
del(x[:]); print(x) #removes all, now the list is empty
x = list(range(10)); print(x)
del(x); print(x) #deletes list, can not print anymore
If we want to remove all occurrences of a particular value, we can use the following code.
x.count(<item>) gives the number of times one item appears in the list.
x = list(range(5)); x.extend(x); print(x) #extend x by itself, double the list
while x.count(3) > 0: x.remove(3)
print(x)
We can also parameterize the list and item in a function.
Functions will be covered later.
def remove_all(x, i): #list x, item i
while x.count(i)>0 : x.remove(i)
x = list(range(5)); x.extend(x); print(x) #extend x by x
remove_all(x,3); print(x)