1
00:00:00 --> 00:00:04
Hello friends and Welcome to the tutorial on 'Manipulating Lists'.
2
00:00:05 --> 00:00:16
At the end of this tutorial, you will be able to,
Concatenate two lists
Learn the details of slicing and striding of lists
Sort and reverse lists.
3
00:00:17 --> 00:00:23
So, before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with Lists".
4
00:00:24 --> 00:00:27
So let's start i python on our terminal
5
00:00:28 --> 00:00:34
Type ipython and hit enter.
6
00:00:35 --> 00:00:47
We have already learn't about lists in Python, how to access individual elements in the list and some of the functions that can be run on the lists like max, min, sum, len and so on.
7
00:00:48 --> 00:00:53
Now let us learn some of the basic operations that can be performed on Lists.
8
00:00:54 --> 00:00:56
We already know how to access individual elements in a List.
9
00:00:57 --> 00:01:06
But what if we have a scenario where we need to get a part of the entire list or what we call as a slice of the list?
10
00:01:07 --> 00:01:09
Python supports slicing on lists.
11
00:01:10 --> 00:01:32
Let us say I have the list, primes equal within bracket 2,3,5,7,11,13,17,19,23,29.
12
00:01:33 --> 00:01:34
Hit enter
13
00:01:35 --> 00:01:50
To obtain all the primes between 10 and 20 from the above list of primes we say primes[4:8]
14
00:01:51 --> 00:02:03
This gives us all the elements in the list starting from the element with the index 4, which is 11, up to the element with index 8 in the list but not including the eighth element.
15
00:02:04 --> 00:02:07
So we obtain a slice starting from 11 up to 19th.
16
00:02:08 --> 00:02:17
It is very important to remember that whenever we specify a range of elements in Python, the start index is included and end index is not included.
17
00:02:18 --> 00:02:28
So in the above case, 11, which is element with the index 4, was included but 23 which was the element with index 8 was excluded.
18
00:02:29 --> 00:02:35
Pause the video here, try out the following exercise and resume the video.
19
00:02:36 --> 00:02:44
Obtain the primes less than 10, from the list primes .
20
00:02:45 --> 00:02:46
So now switch to terminal for solution.
21
00:02:47 --> 00:02:56
Type primes within brackets 0 colon 4 and hit enter.
22
00:02:57 --> 00:02:59
It give us the primes below 10.
23
00:03:00 --> 00:03:16
Generalizing, we can obtain a slice of the list "p" from the index "start" up to the index "end" but excluding "end" with the syntax p[start:stop]
24
00:03:17 --> 00:03:23
So by default the slice fetches all the elements between start and stop including start but not stop.
25
00:03:24 --> 00:03:30
So as to say we obtain all the elements between start and stop in steps of one.
26
00:03:31 --> 00:03:36
Python also provides us the functionality to specify the steps in which the slice must be obtained.
27
00:03:37 --> 00:03:58
So we have num is equal to within brackets 0,1,2,3,4,5,6,7,8,9,10,11,12,13.
28
00:03:59 --> 00:04:11
If we want to obtain all the odd numbers less than 10 from the list num we have to start from element with index 1 upto the index 10 in steps of 2
29
00:04:12 --> 00:04:23
So we type num within square brackets 1 is to 10 is to 2 that is ,1 colon 10 colon 2
30
00:04:24 --> 00:04:27
When no step is specified, it is assumed to be 1.
31
00:04:28 --> 00:04:33
Similarly, there are default values for start and stop in dices as well.
32
00:04:34 --> 00:04:45
If we don't specify the start index, it is implicitly taken as the first element of the list
33
00:04:46 --> 00:04:51
So type num within square brackets colon 10.
34
00:04:52 --> 00:04:58
This gives us all the elements from the beginning upto the 10th element but not including the 10th element in the list.
35
00:04:59 --> 00:05:09
Similarly if the stop index is not specified, it is implicitly assumed to be the end of the list, including the last element of the list
36
00:05:10 --> 00:05:20
So type num within square brackets 10 colon.
37
00:05:21 --> 00:05:29
This gives us all elements starting from the 10th element in the list "num" upto the final element including the last element.
38
00:05:30 --> 00:05:41
To get all the even numbers in the list "num", we do num in square brackets colon colon 2
39
00:05:42 --> 00:05:47
So now pause the video here, try out the following exercise and resume the video.
40
00:05:48 --> 00:05:55
Obtain all the multiples of three from the list num
41
00:05:56 --> 00:05:58
The solution is on your screen.
42
00:05:59 --> 00:06:04
num colon colon 3 gives us all the multiples of 3 from the list.
43
00:06:05 --> 00:06:13
Since every third element in this starting from zero is divisible by 3.
44
00:06:14 --> 00:06:21
The other basic operation that we can perform on lists is concatenation of two or more lists.
45
00:06:22 --> 00:06:25
We can combine two lists by using the "plus" operator.
46
00:06:26 --> 00:06:41
Say we have a is equal to within square brackets 1,2,3,4, b is equal to within square brackets 4,5,6,7
47
00:06:42 --> 00:06:46
Then type a plus b and hit enter.
48
00:06:47 --> 00:06:51
When we concatenate lists using the "plus" operator we get a new list.
49
00:06:52 --> 00:06:57
We can store this list in a new variable,say c,
50
00:06:58 --> 00:07:00
So c is equal to a plus b.
51
00:07:01 --> 00:07:05
Then type c.
52
00:07:06 --> 00:07:12
It is important to observe that the "plus" operator always returns a new list without altering the lists being concatenated in any way.
53
00:07:13 --> 00:07:15
We know that a list is a collection of data.
54
00:07:16 --> 00:07:21
Whenever we have a collection, we run into situations where we want to sort the collection.
55
00:07:22 --> 00:07:27
Lists support sort method which sorts the list in place
56
00:07:28 --> 00:07:39
So type a is equal to 5,1,6,7,7,10.
57
00:07:40 --> 00:07:45
Then type a dot sort and closing brackets.
58
00:07:46 --> 00:07:49
Now the contents of the list a will be
59
00:07:50 --> 00:07:51
Type a and hit enter.
60
00:07:52 --> 00:07:59
As the sort method sorts the elements of a list, the original list we had, is overwritten or replaced.
61
00:08:00 --> 00:08:02
We have no way to obtain the original list back.
62
00:08:03 --> 00:08:09
One way to avoid this is to keep a copy of the original list in another variable and run the sort method on the list.
63
00:08:10 --> 00:08:19
However Python also provides a built-in function called sorted which sorts the list which is passed as an argument to it and returns a new sorted list.
64
00:08:20 --> 00:08:25
We can store this sorted list into another list variable
65
00:08:26 --> 00:08:38
so type a is equal to 5,1 6,7,7,10 and hit enter
66
00:08:39 --> 00:08:46
then type sorted within brackets a, enter
67
00:08:47 --> 00:08:51
Now,as we said we can store the sorted list into another list variable.
68
00:08:52 --> 00:08:58
So type sa is equal to sorted within brackets a.
69
00:08:59 --> 00:09:02
Python also provides the reverse method which reverses the list in place
70
00:09:03 --> 00:09:09
so type a is equal to brackets 1,2,3,4,5
71
00:09:10 --> 00:09:15
a dot reverse within closing brackets.
72
00:09:16 --> 00:09:26
the reverse method reverses the list "a" and stores the reversed list in place i.e. in "a" itself. Lets see the list "a"
73
00:09:27 --> 00:09:29
So type a and hit enter.
74
00:09:30 --> 00:09:31
But again the original list is lost.
75
00:09:32 --> 00:09:36
To reverse a list, we could use striding with negative indexing.
76
00:09:37 --> 00:09:44
So type a within square brackets colon colon -1.
77
00:09:45 --> 00:09:48
We can also store this new reversed list in another list variable.
78
00:09:49 --> 00:09:53
Pause the video here, and try out the exercise and resume the video.
79
00:09:54 --> 00:10:00
Given a list of marks of students in an examination, obtain a list with marks in descending order.
80
00:10:01 --> 00:10:08
marks are 99, 67, 47, 100, 50, 75, 62.
81
00:10:09 --> 00:10:16
So now switch to terminal
82
00:10:17 --> 00:10:35
and type marks is equal to within square brackets 99,67,47,100,50,75,62.
83
00:10:36 --> 00:10:53
Now, in terminal type sorted within brackets marks then in square brackets colon colon minus 1.
84
00:10:54 --> 00:10:56
Then or you can use another method
85
00:10:57 --> 00:11:07
So sorted within brackets marks comma reverse is equal to True.
86
00:11:08 --> 00:11:10
So this brings us to the end of this tutorial.
87
00:11:11 --> 00:11:13
In this tutorial, we have learn't to,
88
00:11:14 --> 00:11:16
obtain parts of lists using slicing and striding.
89
00:11:17 --> 00:11:19
Concatenate lists using the plus operator.
90
00:11:20 --> 00:11:21
3. Sort lists using the sort method.
91
00:11:22 --> 00:11:25
4. Use the method reverse to reverse the lists.
92
00:11:26 --> 00:11:29
Here are some self assessment questions for you to solve
93
00:11:30 --> 00:11:40
Given the list primes, primes is equal to 1, 3, 5, 7, 11, 13, 17, 19, 23, 29 , How do you obtain the last 4 primes?
94
00:11:41 --> 00:11:52
Given a list, p, of unknown length, obtain the first 3 (or all, if there are fewer) characteristics of it.
95
00:11:53 --> 00:11:57
reversed function reverses a list in place. True or False?
96
00:11:58 --> 00:12:02
Let us look at the answers
97
00:12:03 --> 00:12:04
And the answers are
98
00:12:05 --> 00:12:08
The last four primes can be obtained from the given list as,
99
00:12:09 --> 00:12:13
primes within brackets -4 colon.
100
00:12:14 --> 00:12:19
and The first 3 characters can be obtained as,
101
00:12:20 --> 00:12:23
p within square brackets colon 3.
102
00:12:24 --> 00:12:27
and the third and the final question the answer is False.
103
00:12:28 --> 00:12:31
The function reverse will reverse a list in place.
104
00:12:32 --> 00:12:34
Hope you have enjoyed this tutorial and found it useful.
105
00:12:35 --> 00:12:40
Thank you!