filmov
tv
Python with Jupyter Notebook Tutorial - Lists & Tuples (HD) Part 5
![preview_player](https://i.ytimg.com/vi/xvHntZ9qCqs/hqdefault.jpg)
Показать описание
Tuple: A tuple is a collection of values and it is declared using parentheses. However, we can also use a tuple packing to do the same, and unpacking to assign its values to a sequence.
Lists: Unlike in C++, we don’t have arrays to work with in Python. Here, we have a list instead.
Mutability
a. A List is Mutable
Let’s first see lists. Let’s take a new list for exemplar purposes.
list1=[0,1,2,3,4,5,6,7]
Now first, we’ll try reassigning an element of a list. Let’s reassign the second element to hold the value 3.
list1[1]=3
list1
[0, 3, 2, 3, 4, 5, 6, 7]
Again, let’s see how we can reassign the entire list.
list1=[7,6,5,4,3,2,1,0]
list1
[7, 6, 5, 4, 3, 2, 1, 0]
It worked, great.
Now, we will delete just one element from the list.
del list1[1]
list1
[7, 5, 4, 3, 2, 1, 0]
This was easy, but could we delete a slice of the list? Let’s try it.
del list1[3:]
list1
[7, 5, 4]
We can access a slice the same way. Can we reassign a slice?
nums=[1,2,3,4,5]
nums[1:3]=[6,7,8]
nums
[1, 6, 7, 8, 4, 5]
Indeed, we can. Finally, let’s try deleting the entire list.
del list1
list1
Traceback (most recent call last):
File "pyshell#67", line 1, in module
list1
NameError: name ‘list1’ is not defined
The list doesn’t exist anymore.
b. A Tuple is Immutable
Now, let’s try doing the same things to a tuple. We know that a tuple is immutable, so some of these operations shouldn’t work. We’ll take a new tuple for this purpose.
mytuple=0,1,2,3,4,5,6,7
First, let’s try reassigning the second element.
mytuple[1]=3
Traceback (most recent call last):
File "pyshell#70", line 1, in module
mytuple[1]=3
TypeError: ‘tuple’ object does not support item assignment
As you can see, a tuple doesn’t support item assignment.
However, we can reassign an entire tuple.
mytuple=2,3,4,5,6
mytuple
(2, 3, 4, 5, 6)
Next, let’s try slicing a tuple to access or delete it.
mytuple[3:]
(5, 6)
del mytuple[3:]
Traceback (most recent call last):
File "pyshell#74", line 1, in module
del mytuple[3:]
TypeError: ‘tuple’ object does not support item deletion
As is visible, we can slice it to access it, but we can’t delete a slice. This is because it is immutable.
Can we delete a single element?
del mytuple[3]
Traceback (most recent call last):
File "pyshell#75", line 1, in module
del mytuple[3]
TypeError: ‘tuple’ object doesn’t support item deletion
Apparently, the answer is no.
Finally, let’s try deleting the entire tuple.
del mytuple
mytuple
Traceback (most recent call last):
File "pyshell#77", line 1, in module
mytuple
NameError: name ‘mytuple’ is not defined
So, here, we conclude that you can slice a tuple, reassign it whole, or delete it whole. But you cannot delete or reassign just a few elements or a slice.
Let us proceed with more differences between python tuples vs lists.
Functions
Some python functions apply on both, these are- len(), max(), min(), sum(), any(), all(), sorted(). We’ll take just one example here for both containers.
max((1,3,-1))
3
max([1,3,-1])
3
Methods
Lists and tuples share the index() and count() methods. But other than those, there are a few methods that apply to lists. These are- append(), insert(), remove(), pop(), clear(), sort(), and reverse(). Let’s take an example of one of these.
[1,3,2].index(3)
1
(1,3,2).index(3)
1
To get an insight into all of these methods and functions we mentioned, you should refer to our articles on lists and tuples.
Tuples in a List
We can store tuples in a list when we want to.
mylist=[(1,2,3),(4,5,6)]
type(mylist)
class 'list'
type(mylist[1])
class ‘tuple’
But when would we need to do this? Take an example.
list(students)
[(1, ‘ABC’), (2, ‘DEF’), (3, ‘GHI’)]
Lists in a Tuple
Likewise, we can also use a tuple to store lists. Let’s see how.
mytuple=([1,2],[3,4],[5,6])
Nested Tuples
A tuple may hold more tuples, and this can go on in more than two dimensions.
mytuple=((1,2),(3,(4,5),(6,(7,(8,9)))))
To access the element with the value 8, we write the following code.
mytuple[1][2][1][1][0]
8
Nested Lists
Similarly, a list may hold more lists, in as many dimensions as you want.
mylist=[[1,2],[3,4]]
myotherlist=[[1,2],[3,[4,5]]]
To access the element with the value 5, we write the following c ode.
myotherlist[1][1][1]
5
Lists: Unlike in C++, we don’t have arrays to work with in Python. Here, we have a list instead.
Mutability
a. A List is Mutable
Let’s first see lists. Let’s take a new list for exemplar purposes.
list1=[0,1,2,3,4,5,6,7]
Now first, we’ll try reassigning an element of a list. Let’s reassign the second element to hold the value 3.
list1[1]=3
list1
[0, 3, 2, 3, 4, 5, 6, 7]
Again, let’s see how we can reassign the entire list.
list1=[7,6,5,4,3,2,1,0]
list1
[7, 6, 5, 4, 3, 2, 1, 0]
It worked, great.
Now, we will delete just one element from the list.
del list1[1]
list1
[7, 5, 4, 3, 2, 1, 0]
This was easy, but could we delete a slice of the list? Let’s try it.
del list1[3:]
list1
[7, 5, 4]
We can access a slice the same way. Can we reassign a slice?
nums=[1,2,3,4,5]
nums[1:3]=[6,7,8]
nums
[1, 6, 7, 8, 4, 5]
Indeed, we can. Finally, let’s try deleting the entire list.
del list1
list1
Traceback (most recent call last):
File "pyshell#67", line 1, in module
list1
NameError: name ‘list1’ is not defined
The list doesn’t exist anymore.
b. A Tuple is Immutable
Now, let’s try doing the same things to a tuple. We know that a tuple is immutable, so some of these operations shouldn’t work. We’ll take a new tuple for this purpose.
mytuple=0,1,2,3,4,5,6,7
First, let’s try reassigning the second element.
mytuple[1]=3
Traceback (most recent call last):
File "pyshell#70", line 1, in module
mytuple[1]=3
TypeError: ‘tuple’ object does not support item assignment
As you can see, a tuple doesn’t support item assignment.
However, we can reassign an entire tuple.
mytuple=2,3,4,5,6
mytuple
(2, 3, 4, 5, 6)
Next, let’s try slicing a tuple to access or delete it.
mytuple[3:]
(5, 6)
del mytuple[3:]
Traceback (most recent call last):
File "pyshell#74", line 1, in module
del mytuple[3:]
TypeError: ‘tuple’ object does not support item deletion
As is visible, we can slice it to access it, but we can’t delete a slice. This is because it is immutable.
Can we delete a single element?
del mytuple[3]
Traceback (most recent call last):
File "pyshell#75", line 1, in module
del mytuple[3]
TypeError: ‘tuple’ object doesn’t support item deletion
Apparently, the answer is no.
Finally, let’s try deleting the entire tuple.
del mytuple
mytuple
Traceback (most recent call last):
File "pyshell#77", line 1, in module
mytuple
NameError: name ‘mytuple’ is not defined
So, here, we conclude that you can slice a tuple, reassign it whole, or delete it whole. But you cannot delete or reassign just a few elements or a slice.
Let us proceed with more differences between python tuples vs lists.
Functions
Some python functions apply on both, these are- len(), max(), min(), sum(), any(), all(), sorted(). We’ll take just one example here for both containers.
max((1,3,-1))
3
max([1,3,-1])
3
Methods
Lists and tuples share the index() and count() methods. But other than those, there are a few methods that apply to lists. These are- append(), insert(), remove(), pop(), clear(), sort(), and reverse(). Let’s take an example of one of these.
[1,3,2].index(3)
1
(1,3,2).index(3)
1
To get an insight into all of these methods and functions we mentioned, you should refer to our articles on lists and tuples.
Tuples in a List
We can store tuples in a list when we want to.
mylist=[(1,2,3),(4,5,6)]
type(mylist)
class 'list'
type(mylist[1])
class ‘tuple’
But when would we need to do this? Take an example.
list(students)
[(1, ‘ABC’), (2, ‘DEF’), (3, ‘GHI’)]
Lists in a Tuple
Likewise, we can also use a tuple to store lists. Let’s see how.
mytuple=([1,2],[3,4],[5,6])
Nested Tuples
A tuple may hold more tuples, and this can go on in more than two dimensions.
mytuple=((1,2),(3,(4,5),(6,(7,(8,9)))))
To access the element with the value 8, we write the following code.
mytuple[1][2][1][1][0]
8
Nested Lists
Similarly, a list may hold more lists, in as many dimensions as you want.
mylist=[[1,2],[3,4]]
myotherlist=[[1,2],[3,[4,5]]]
To access the element with the value 5, we write the following c ode.
myotherlist[1][1][1]
5
Комментарии