1.4 lists
Contents
Note
Click here to download the full example code or to run this example in your browser via Binder
1.4 lists#
This lesson describes the concept of list in python.
A list can be defined as a collection of objects or a container in which we hold different objects.
mylist = []
Above we defined an empty list. How do we know that it is list? We can always check the type of object in python as below.
print(type(mylist))
<class 'list'>
We can also make a list which is not empty as below
imperialists = ['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola']
print(type(imperialists))
<class 'list'>
In above-mentioned list, all of its (6) elements are strings. However, the elements/members a list are not required to be of same type.
imperialists = ["Bush", {"years": 8}, 2000, (2000, 2008)]
print(type(imperialists))
<class 'list'>
In above list, the first member is string, the second member is dictionary,
the third member is integer and the fourth member is a tuple. We will
study about string, dictionary, integer and tuple in upcoming lessons.
- There are two ways to convert a python object into list
using
[]usnig
listfunction
a = 1,2
print(type(a))
a_as_list_using_slice_op = [a]
print(type(a_as_list_using_slice_op))
<class 'tuple'>
<class 'list'>
a_as_list_using_list_fn = list(a)
print(type(a_as_list_using_list_fn))
<class 'list'>
However there is a major difference in these two. [] converts the whole object as it is
into a list. On the other hand, list function is more like element wise operator.
This can be verified by printing the converted lists created above.
print(a_as_list_using_slice_op)
print(a_as_list_using_list_fn)
[(1, 2)]
[1, 2]
a_as_list_using_list_fn is a list with two members while a_as_list_using_slice_op is a list with only one member. This can be verified by checking the length of both lists.
print(len(a_as_list_using_slice_op))
print(len(a_as_list_using_list_fn))
1
2
Then length of list which is created using slice operator [] is always 1, while
the length of list created using list function depends upon the number of values
in the object. The slice operator is creating a list with one member and that one member
is a tuple in this case.
Spend some minutes on understanding the difference between these two ways of creating a list. Try with different objects and see the difference.
Question Write code to prove the above statement.
slicing a list#
Since the list is a sequence, we can slice it as well. The slicing of a list
can be done using the slice operator [].
Consider the following list
imperialists = ['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola']
print(imperialists[0])
Bush
print(imperialists[2])
Trump
print(imperialists[-1])
coca cola
The : operator/symbol is used to slice a list. The syntax is as follows
list[start:stop:step]. The default value of start is 0, of stop is
length of list and of step is 1.
print(imperialists[2:4])
['Trump', 'Zuckerberg']
Question: What will be the output of the following code
Operations on a list#
Once we have a list, we can perform different operations on it. Some of them are given below.
append#
imperialists = ['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola']
print(imperialists)
['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola']
imperialists.append('clinton')
print(imperialists)
['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola', 'clinton']
append changes the original list and it itself returns None.
new_imperialists = imperialists.append('netanyahu')
print(new_imperialists)
None
print(imperialists)
['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola', 'clinton', 'netanyahu']
If we add a similar but new element in list, then the list will have 2 such elements as its member.
imperialists.append('netanyahu')
print(imperialists)
['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola', 'clinton', 'netanyahu', 'netanyahu']
Question: What will be the output of the following code?
pop#
last_element = imperialists.pop(-1)
print(imperialists)
['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola', 'clinton', 'netanyahu']
print(last_element)
netanyahu
# uncomment following 1 line
# imperialists.pop(8)
imperialists.pop()
'netanyahu'
print(imperialists)
['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola', 'clinton']
# uncomment following 1 line
# imperialists.pop('Bush')
Question: What will be the output of the following code?
extend#
If we want to add multiple elements to a list, using append will put a new list in the previous list
imperialists = ['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola', 'clinton']
uk_imperialists = ['churchil', 'Tony Blair', 'BBC']
imperialists.append(uk_imperialists)
print(imperialists)
['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola', 'clinton', ['churchil', 'Tony Blair', 'BBC']]
imperialists = ['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola', 'clinton']
imperialists.extend(uk_imperialists)
print(imperialists)
['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola', 'clinton', 'churchil', 'Tony Blair', 'BBC']
extend actually takes any sequence as input. It must not be a list. It can
be a string or tuple.
new_one = "Murdoch"
imperialists.extend(new_one)
print(imperialists)
['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola', 'clinton', 'churchil', 'Tony Blair', 'BBC', 'M', 'u', 'r', 'd', 'o', 'c', 'h']
imperialists = ['Bush', 'Obama', 'Trump', 'clinton']
capitalists = ('Zuckerberg', 'Bezos', 'coca cola')
imperialists.extend(capitalists)
print(imperialists)
['Bush', 'Obama', 'Trump', 'clinton', 'Zuckerberg', 'Bezos', 'coca cola']
Question: What will be the output of the following code?
using + operator#
We can also append lists using the + operator.
media_houses = ['bbc', 'cnn', 'reuters', 'springer']
print(imperialists + media_houses)
['Bush', 'Obama', 'Trump', 'clinton', 'Zuckerberg', 'Bezos', 'coca cola', 'bbc', 'cnn', 'reuters', 'springer']
So when we use + operator between two lists, a new list is created which contains all the members of both lists. The original lists remain unchanged.
print(imperialists)
['Bush', 'Obama', 'Trump', 'clinton', 'Zuckerberg', 'Bezos', 'coca cola']
imperialists = imperialists + media_houses
print(imperialists)
['Bush', 'Obama', 'Trump', 'clinton', 'Zuckerberg', 'Bezos', 'coca cola', 'bbc', 'cnn', 'reuters', 'springer']
Above we are recreating the list imperialists by adding media_houses to it.
morons = ['sam haris', 'richard dawkins', 'baghdadi', 'bin ladan']
imperialists += morons
print(imperialists)
['Bush', 'Obama', 'Trump', 'clinton', 'Zuckerberg', 'Bezos', 'coca cola', 'bbc', 'cnn', 'reuters', 'springer', 'sam haris', 'richard dawkins', 'baghdadi', 'bin ladan']
+= operator means that the list on the left side of += operator is updated by adding the list on the right side of += operator.
Question: What will be the output of the following code?
remove#
imperialists.remove('springer')
print(imperialists)
['Bush', 'Obama', 'Trump', 'clinton', 'Zuckerberg', 'Bezos', 'coca cola', 'bbc', 'cnn', 'reuters', 'sam haris', 'richard dawkins', 'baghdadi', 'bin ladan']
If we repeat the above operation, it will result in error because springer has already been removed from the list imperialists.
# uncomment following 2 line
# imperialists.remove('springer')
# print(imperialists)
insert#
puts an the value before the index
imperialists.insert(-1, 'DW')
print(imperialists)
['Bush', 'Obama', 'Trump', 'clinton', 'Zuckerberg', 'Bezos', 'coca cola', 'bbc', 'cnn', 'reuters', 'sam haris', 'richard dawkins', 'baghdadi', 'DW', 'bin ladan']
Finding position/index of a member of a list#
imperialists.index('bbc')
7
# uncomment following 1 line
# imperialists.index('bbc', 8)
# uncomment following 1 line
# imperialists.index('bbc', 3, 6)
if an element is present in a list twice, index of its first position is returned.
last_value = imperialists[-1]
imperialists.insert(2, last_value)
print(imperialists)
['Bush', 'Obama', 'bin ladan', 'Trump', 'clinton', 'Zuckerberg', 'Bezos', 'coca cola', 'bbc', 'cnn', 'reuters', 'sam haris', 'richard dawkins', 'baghdadi', 'DW', 'bin ladan']
imperialists.index(last_value)
2
Question: What will be the output of the following code?
reverse#
imperialists = ['bbc', 'cnn', 'reuters', 'springer', 'voa']
imperialists.reverse()
The function does not return anything itself but the original list is reversed.
print(imperialists)
['voa', 'springer', 'reuters', 'cnn', 'bbc']
Question: What will be the output of the following code?
- sort
We can sort the list using the sort function. If the contents of the list strings, then the list will be sorted in lexicographical order. If the contents are numbers, then the list will be sorted in ascending order.
print(imperialists)
['voa', 'springer', 'reuters', 'cnn', 'bbc']
The function does not return anything itself but the original list is sorted.
imperialists.sort()
print(imperialists)
['bbc', 'cnn', 'reuters', 'springer', 'voa']
imperialists.sort(reverse=True)
print(imperialists)
['voa', 'springer', 'reuters', 'cnn', 'bbc']
[2, 3, 4, 6]
# uncomment following 2 line
# imperialists = ['bbc', 1, 'cnn', 3, 'voa', 2]
# imperialists.sort()
Question What will be the output of the following code
x = [1,2]
y = [3,4, 5]
print(len(x + y))
*#
['Najaf', 'Najaf', 'Najaf']
['Najaf', '->', 'Karbala', 'Najaf', '->', 'Karbala', 'Najaf', '->', 'Karbala']
Notes#
I have been using the word function for append, sort etc. However, in
Object-Oriented Programming, it can be seen that they are actually called methods.
There are a lot more powerful list manipulations which can be done by combining conditional and looping statements. We will come back to them once looping and conditioning statements are covered.
Total running time of the script: ( 0 minutes 0.014 seconds)