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 lists in python.
Important
This lesson is still under development.
A list can be defined as a collection of objects or a container in which we hold different objects.
mylist = []
Above we an empty list. How do we know that it is list? We can always check the type of an 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 need not 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
list
function
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]
print(len(a_as_list_using_slice_op))
print(len(a_as_list_using_list_fn))
1
2
Then length of list using slice operator []
is always 1, while
the length of list created using list
function depends upon the number of values
in the object.
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']
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')
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']
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']
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']
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']
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
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']
sort#
print(imperialists)
['voa', 'springer', 'reuters', 'cnn', 'bbc']
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()
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.012 seconds)