Python with Jupyter Notebook Tutorial - Lists & Tuples (HD) Part 5
This tutorial covers tuples in Python, including their declaration with parentheses, tuple packing, and unpacking techniques, demonstrated within Jupyter Notebook.

In Layman's Terms
1.0K views ⢠Apr 2, 2019

About this video
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
Tags and Topics
Browse our collection to discover more content in these categories.
Video Information
Views
1.0K
Likes
7
Duration
7:02
Published
Apr 2, 2019
User Reviews
4.0
(1) Related Trending Topics
LIVE TRENDSRelated trending topics. Click any trend to explore more videos.
No specific trending topics match this video yet.
Explore All Trends