1
00:00:00,000 --> 00:00:04,990
Tuples and lists are very powerful in Python and there are a lot of operations

2
00:00:05,000 --> 00:00:06,990
that you can perform on them.

3
00:00:07,000 --> 00:00:10,990
Let's go ahead and set up a couple of tuples and lists.

4
00:00:11,000 --> 00:00:19,990
We'll have a tuple we'll call t and we'll initialize it with a range.

5
00:00:20,000 --> 00:00:24,990
So, there is our tuple and we can see type(t) is class tuple.

6
00:00:25,000 --> 00:00:30,990
So, I can check and I can say is there 10 in there. I can say 10 in t and I'll get it True.

7
00:00:31,000 --> 00:00:33,990
If I say 50 in t I get a false.

8
00:00:34,000 --> 00:00:40,990
Of course, you can also use not in. I can say 50 not in t and that will be True.

9
00:00:41,000 --> 00:00:44,990
I can look at a particular entry. I can say well what is that index 10.

10
00:00:45,000 --> 00:00:48,990
I can say t10 like that.

11
00:00:49,000 --> 00:00:52,990
I see because a range was 0 base that at position 10 is the number 10.

12
00:00:53,000 --> 00:00:59,990
Of course, I can look at the length of it and a tuple is iterable.

13
00:01:00,000 --> 00:01:06,990
I can say for i in t and I can print(i).

14
00:01:07,000 --> 00:01:15,990
Likewise, you can do all of these things with lists. x= list(range(20).

15
00:01:16,000 --> 00:01:17,990
I am missing a parenthesis there.

16
00:01:18,000 --> 00:01:23,990
So x is now that list and I can 10 in x and I can get true.

17
00:01:24,000 --> 00:01:30,990
I can 20 in x and will get false because that range was not inclusive and I can

18
00:01:31,000 --> 00:01:33,990
iterate it. I can say for i in x:

19
00:01:34,000 --> 00:01:37,990
print(i) and that's iterable as well.

20
00:01:38,000 --> 00:01:42,990
Of course, one of the major differences is that I can assign things to my list and

21
00:01:43,000 --> 00:01:44,990
cannot assign things to my tuple.

22
00:01:45,000 --> 00:01:46,990
My tuple is immutable.

23
00:01:47,000 --> 00:01:53,990
So I can't say t sub 10 = 25.

24
00:01:54,000 --> 00:01:55,990
tuple object does not support item assignment.

25
00:01:56,000 --> 00:01:57,990
A tuple is immutable.

26
00:01:58,000 --> 00:02:05,990
But I can say x sub 10 = 25 and when I look at x now I've got that 25 in the middle of it.

27
00:02:06,000 --> 00:02:11,990
Likewise, I can do any operation on a tuple that does not involve modifying it.

28
00:02:12,000 --> 00:02:17,990
I can use the count method to count how many 5s there are in the tuple and there

29
00:02:18,000 --> 00:02:19,990
is of course one of them.

30
00:02:20,000 --> 00:02:20,990
I can find the index of the 5.

31
00:02:21,000 --> 00:02:28,990
That will find that index of the first occurrence of that value and it's index number 5.

32
00:02:29,000 --> 00:02:36,990
If we look at t here we see 0, 1, 2, 4, 5. So that one there is indeed a 5.

33
00:02:37,000 --> 00:02:44,990
But I cannot append. If I say t.append(100), 'tuple' object has no

34
00:02:45,000 --> 00:02:46,990
attribute 'append'.

35
00:02:47,000 --> 00:02:50,990
On the other hand, if I look at my x object, which is a list, and I say

36
00:02:51,000 --> 00:02:56,990
x.append(100), it will put 100 at the end of it.

37
00:02:57,000 --> 00:03:00,990
The length of my x object is 21 instead of 20.

38
00:03:01,000 --> 00:03:04,990
I've actually extended it because I've added something to the end of it.

39
00:03:05,000 --> 00:03:15,990
If I want to add a number of things, I can't do that with append but I can do it with extend.

40
00:03:16,000 --> 00:03:19,990
This will allow me to extend it with any iterable object, so if I say

41
00:03:20,000 --> 00:03:26,990
(range(20)) now I'll get another 20 objects at the end of it.

42
00:03:27,000 --> 00:03:32,990
So, that range has been appended to the end using the extend method.

43
00:03:33,000 --> 00:03:38,990
If I want to insert something, I can say x.insert and I can insert at the

44
00:03:39,000 --> 00:03:45,990
beginning if I want to and I can put a 25 there and now we look at x.

45
00:03:46,000 --> 00:03:50,990
We've again extended the length of the array and there is a 25 at the beginning.

46
00:03:51,000 --> 00:03:52,990
But I can insert any place I want to.

47
00:03:53,000 --> 00:04:01,990
I can say x.insert(12, 100) and we'll see that I've got 100 there in the

48
00:04:02,000 --> 00:04:04,990
middle of the list.

49
00:04:05,000 --> 00:04:06,990
Likewise, I can remove it.

50
00:04:07,000 --> 00:04:12,990
x.remove(12) and that will remove the element with the value 12.

51
00:04:13,000 --> 00:04:17,990
It actually looks for the first value of 12 and removes that.

52
00:04:18,000 --> 00:04:27,990
If I want to delete the item at index 12 I use the delete operator and say x12 and

53
00:04:28,000 --> 00:04:31,990
that will delete that element at index 12.

54
00:04:32,000 --> 00:04:38,990
So, we see that 100 that we added in and we inserted at 12 is now gone.

55
00:04:39,000 --> 00:04:43,990
Finally, just like I can insert things at the beginning or at the end I can

56
00:04:44,000 --> 00:04:45,990
pop to remove things.

57
00:04:46,000 --> 00:04:53,990
If I just say x.pop, it will give me the value from the very end of the list and it removes it.

58
00:04:54,000 --> 00:05:00,990
So, you see that 19 is now gone. I got it returned from the pop method and now it's gone.

59
00:05:01,000 --> 00:05:07,990
If I want to, I can pop from the beginning I can say x.pop(0) and that will

60
00:05:08,000 --> 00:05:08,990
pop from the beginning.

61
00:05:09,000 --> 00:05:13,990
Now, I got that 25 from the beginning and the list is shorter by 1 and that's

62
00:05:14,000 --> 00:05:15,990
been removed from the list.

63
00:05:16,000 --> 00:05:20,990
Basically, we can do anything we want to with these lists, as long as we are

64
00:05:21,000 --> 00:05:22,990
using the mutable list object.

65
00:05:23,000 --> 00:05:28,990
So, all the power is there. You've got the equivalents of push and pop and insert and remove.

66
00:05:29,000 --> 00:05:33,990
You can remove things based on their value or based on their index, you can

67
00:05:34,000 --> 00:05:37,990
search for things, you can count the occurrence of things.

68
00:05:38,000 --> 00:05:41,990
All of this power is here for both the tuple and the list, as long as you are not

69
00:05:42,000 --> 00:05:45,990
using the things on the tuple that would modify it. Because you have to remember

70
00:05:46,000 --> 00:05:56,000
the tuple is immutable and the list is mutable.

