< Return to Video

Python Lists, Tuples And Dictionaries - 10 | Python For Beginners | Python Tutorial | Simplilearn

  • 0:00 - 0:04
    [MUSIC PLAYING]
  • 0:04 - 0:08
  • 0:08 - 0:09
    INSTRUCTOR: Hello, everyone.
  • 0:09 - 0:12
    Welcome to another video on
    Python series of tutorials
  • 0:12 - 0:13
    by Simplilearn.
  • 0:13 - 0:17
    In this session, we will look at
    lists, tuples, and dictionaries
  • 0:17 - 0:18
    in Python.
  • 0:18 - 0:20
    Now let us see the
    agenda for today.
  • 0:20 - 0:22
    First, we will learn
    about the lists
  • 0:22 - 0:25
    in Python, its
    characteristics, and explore it
  • 0:25 - 0:26
    in Jupyter Notebook.
  • 0:26 - 0:29
    Then we will learn about tuples
    in Python, its characteristics,
  • 0:29 - 0:32
    and we will also explore
    tuples in Jupyter Notebook.
  • 0:32 - 0:34
    After that, we will have
    a look at dictionaries
  • 0:34 - 0:37
    in Python, its
    characteristics, and will
  • 0:37 - 0:40
    head to Jupyter Notebook to
    understand the dictionaries.
  • 0:40 - 0:43
    So let's start with
    lists in Python.
  • 0:43 - 0:46
    Imagine you have
    different values.
  • 0:46 - 0:48
    You have numbers,
    you have strings.
  • 0:48 - 0:50
    Now if you want to
    group them together,
  • 0:50 - 0:53
    you can do this job in Python
    with the help of lists,
  • 0:53 - 0:57
    or you have worked with arrays
    in any other language like C++
  • 0:57 - 0:58
    or Java.
  • 0:58 - 1:02
    This is the similar stuff
    in here, which is list.
  • 1:02 - 1:05
    A list can be defined as a
    collection of objects, values,
  • 1:05 - 1:08
    or items of different
    types, and these collections
  • 1:08 - 1:10
    are enclosed within
    the square brackets
  • 1:10 - 1:12
    and are separated by commas.
  • 1:12 - 1:15
    Let's understand this with
    the help of an example.
  • 1:15 - 1:18
    We will create a list
    and name it as list1,
  • 1:18 - 1:20
    and insert the elements
    in square brackets
  • 1:20 - 1:23
    and separate them with
    the help of commas.
  • 1:23 - 1:26
    List1 is equal to
    square brackets 1
  • 1:26 - 1:31
    comma Adam comma 107 USA.
  • 1:31 - 1:35
    And in lists, we have our
    first element at zeroth index
  • 1:35 - 1:40
    and we have 1 at 0
    index, Adam at one index,
  • 1:40 - 1:45
    107 at second index,
    USA at third index.
  • 1:45 - 1:47
    And talking about
    the reverse index,
  • 1:47 - 1:49
    it starts with
    minus 1 index value,
  • 1:49 - 1:54
    we have USA at minus 1
    index value, 107 at minus 2
  • 1:54 - 1:57
    index value, Adam at
    minus 3 index value,
  • 1:57 - 2:00
    and 1 at minus 4 index value.
  • 2:00 - 2:02
    We will explore the list
    with various examples
  • 2:02 - 2:03
    in Jupyter Notebook in a while.
  • 2:03 - 2:07
    Till then, let's look at the
    characteristics of lists.
  • 2:07 - 2:09
    First, the lists are ordered.
  • 2:09 - 2:11
    When we say that
    lists are ordered,
  • 2:11 - 2:14
    it means that the items
    have a defined order
  • 2:14 - 2:16
    and that order will not change.
  • 2:16 - 2:19
    If you add new items to
    a list, the new items
  • 2:19 - 2:21
    will be placed at
    the end of the list.
  • 2:21 - 2:26
    Second, elements of the list
    can be accessed by index values.
  • 2:26 - 2:29
    Third, lists can store
    various types of elements,
  • 2:29 - 2:33
    for example numbers,
    strings, or lists itself.
  • 2:33 - 2:35
    Fourth, lists are mutable.
  • 2:35 - 2:39
    The list is changeable, meaning
    that we can change, add,
  • 2:39 - 2:43
    and remove items in a list
    after it has been created.
  • 2:43 - 2:45
    Lists allow duplicate elements.
  • 2:45 - 2:47
    Since lists are
    indexed, list can
  • 2:47 - 2:49
    have items with the same value.
  • 2:49 - 2:52
    Let's have a look at some
    methods of lists in Python.
  • 2:52 - 2:55
  • 2:55 - 2:57
    The first one is append.
  • 2:57 - 3:00
    It adds an element at
    the end of the list.
  • 3:00 - 3:03
    For syntax, we can
    write list1.append.
  • 3:03 - 3:06
    And in round brackets, we have
    to write the element which
  • 3:06 - 3:08
    we want to append.
  • 3:08 - 3:09
    The next is insert.
  • 3:09 - 3:12
    It adds an element at
    the specified position.
  • 3:12 - 3:15
    Its syntax is name of
    the list dot insert.
  • 3:15 - 3:17
    And in round brackets,
    we will write the index
  • 3:17 - 3:21
    at which we want to insert
    the element and comma,
  • 3:21 - 3:23
    and after that the element,
    which we want to insert.
  • 3:23 - 3:25
    Third is extend.
  • 3:25 - 3:29
    It adds the element of a list
    to the end of the current list.
  • 3:29 - 3:32
    Its syntax is list1.extend.
  • 3:32 - 3:36
    And in round brackets, we will
    add the element like list2 which
  • 3:36 - 3:38
    we want to extend in the list.
  • 3:38 - 3:40
    The next is index.
  • 3:40 - 3:43
    It returns the index of the
    first element with the specified
  • 3:43 - 3:45
    value.
  • 3:45 - 3:48
    And its syntax is
    list1.index and the element,
  • 3:48 - 3:53
    which we want to fetch
    the index eigtht.
  • 3:53 - 3:54
    The next is remove.
  • 3:54 - 3:57
    It removes the item with
    the specified value.
  • 3:57 - 3:59
    Its syntax is list1.remove.
  • 3:59 - 4:01
    And in round brackets,
    we write the element,
  • 4:01 - 4:04
    which we want to
    remove from the list.
  • 4:04 - 4:05
    The next is sort.
  • 4:05 - 4:08
    This method is used
    to the sort the list.
  • 4:08 - 4:12
    Its syntax is list1.sort.
  • 4:12 - 4:13
    The next is reverse.
  • 4:13 - 4:16
    It reverses the
    order of the list.
  • 4:16 - 4:19
    Its syntax is list1.reverse.
  • 4:19 - 4:24
    Let's move to Jupyter Notebook
    to explore these methods.
  • 4:24 - 4:26
    Let's start with
    creating a list.
  • 4:26 - 4:32
    We'll name the list as list1,
    and we'll create equal to.
  • 4:32 - 4:37
    And we'll start with
    square brackets.
  • 4:37 - 4:38
    And we'll write our
    first element that
  • 4:38 - 4:42
    would be apple comma space.
  • 4:42 - 4:48
    Next element,
    orange comma space.
  • 4:48 - 4:54
    The next element,
    banana comma space.
  • 4:54 - 4:56
    The next element, kiwi.
  • 4:56 - 4:59
  • 4:59 - 5:01
    Now we will get
    this list printed.
  • 5:01 - 5:04
    For that we, will
    write the syntax print.
  • 5:04 - 5:05
    And in round brackets,
    we will write
  • 5:05 - 5:09
    the list name that would be
    list1 and run the command.
  • 5:09 - 5:13
    You can see here that
    the list is printed.
  • 5:13 - 5:14
    Now let's create
    a different list
  • 5:14 - 5:17
    with some different elements.
  • 5:17 - 5:20
    List2 equal to.
  • 5:20 - 5:26
    And in square brackets,
    we will write apple.
  • 5:26 - 5:30
    Now we will insert
    a list in a list.
  • 5:30 - 5:35
    For that, we will write a list
    in square brackets 8 comma 4
  • 5:35 - 5:40
    comma 6 and comma.
  • 5:40 - 5:42
    The next element that
    we will write orange.
  • 5:42 - 5:46
  • 5:46 - 5:47
    The next element banana.
  • 5:47 - 5:56
  • 5:56 - 5:58
    Now we'll print this list.
  • 5:58 - 6:02
    To print this, we'll write print
    round brackets we'll write list2
  • 6:02 - 6:03
    And run the command.
  • 6:03 - 6:07
    Here we will get
    the list2 printed.
  • 6:07 - 6:10
    We can access any
    element in the list.
  • 6:10 - 6:14
    So for that, we will
    write the syntax print.
  • 6:14 - 6:16
    We want to access the
    second element that
  • 6:16 - 6:19
    will be the third
    element from the list1.
  • 6:19 - 6:23
    For that, we will write
    the syntax print list1.
  • 6:23 - 6:25
    And in square brackets,
    we will write the index 2.
  • 6:25 - 6:27
    And run the command.
  • 6:27 - 6:32
    We will get the third element
    from the list1 that is banana.
  • 6:32 - 6:36
    Now we will access an
    element from the list2.
  • 6:36 - 6:42
    For that, we will write
    the command print list2.
  • 6:42 - 6:45
    And in square bracket,
    we will write 1.
  • 6:45 - 6:48
    And in square bracket, we will
    write 2 and run the command.
  • 6:48 - 6:51
    What we get a 6.
  • 6:51 - 6:54
    So to access an element
    from a list that
  • 6:54 - 6:58
    is already inserted in a list,
    we have to write this syntax.
  • 6:58 - 7:00
    So this syntax tells
    us that we will first
  • 7:00 - 7:05
    access the first index element
    that is 8, 4, 6-- the list.
  • 7:05 - 7:08
    And in that, we will access
    the second index value
  • 7:08 - 7:10
    that would be 0, 1, and 2.
  • 7:10 - 7:11
    That is 6.
  • 7:11 - 7:15
    So this is the syntax to access
    an element in a list that
  • 7:15 - 7:17
    is already inserted in a list.
  • 7:17 - 7:20
    Now we will see a
    reverse index order.
  • 7:20 - 7:25
    For that, we will write
    the command print list1.
  • 7:25 - 7:29
    And in square brackets, we
    will write the index minus 1.
  • 7:29 - 7:30
    And we will run this command.
  • 7:30 - 7:34
    And we get kiwi as output.
  • 7:34 - 7:36
    Now we will see
    slicing in Python.
  • 7:36 - 7:38
    We can access a range
    of items in a list
  • 7:38 - 7:42
    by using the slicing operator.
  • 7:42 - 7:46
    Now we will see an example
    to see the slicing operator
  • 7:46 - 7:47
    work in Python.
  • 7:47 - 7:55
    For that, we will write
    the syntax print list1.
  • 7:55 - 8:00
    And in square brackets, we'll
    write 1 slicing operator 3
  • 8:00 - 8:03
    and run the command.
  • 8:03 - 8:09
    What we see is orange and banana
    as an output from the list1.
  • 8:09 - 8:13
    When you write 1 ratio 3, that
    is one slicing operator 3,
  • 8:13 - 8:18
    it gives you the elements
    at index 1, 2, and not 3.
  • 8:18 - 8:23
    So we get the output
    as orange comma banana.
  • 8:23 - 8:29
    Now we will see another example
    for the slicing operator.
  • 8:29 - 8:35
    We'll write print list 1.
  • 8:35 - 8:40
    And in square brackets, we'll
    write 3 and slicing operator
  • 8:40 - 8:42
    and run the command.
  • 8:42 - 8:44
    Here we can get only kiwi.
  • 8:44 - 8:48
    When we write a single number
    before slicing operator,
  • 8:48 - 8:50
    it would print the
    value at that index
  • 8:50 - 8:53
    value and all the other elements
    that are present in the list.
  • 8:53 - 8:57
    But in this list, we have
    only kiwi as the last element,
  • 8:57 - 9:02
    so it would print third index
    value and not more than that.
  • 9:02 - 9:04
    Now we'll see append function.
  • 9:04 - 9:09
    For that, we'll write
    the syntax list1.append.
  • 9:09 - 9:11
    And in round brackets
    we will write
  • 9:11 - 9:15
    the element, which we want to
    insert at the last of the list.
  • 9:15 - 9:20
    We will write guava
    and run it, and now
  • 9:20 - 9:25
    we will print the list
    to see the result.
  • 9:25 - 9:30
    Here we can see that guava is
    added at the last of the list.
  • 9:30 - 9:32
    Now we'll see extend function.
  • 9:32 - 9:36
    For that, we'll write
    the index list1.extend.
  • 9:36 - 9:42
    And in round brackets,
    we will write
  • 9:42 - 9:45
    in square brackets,
    the particular elements
  • 9:45 - 9:47
    we want to insert in the list.
  • 9:47 - 9:52
    So we will write watermelon
    and the next element
  • 9:52 - 9:54
    we want to insert
    would be muskmelon.
  • 9:54 - 9:56
  • 9:56 - 9:58
    That would be in
    inverted commas.
  • 9:58 - 10:03
  • 10:03 - 10:07
    And we also want this
    list to be printed.
  • 10:07 - 10:11
    So we'll write print list1
    and execute this command.
  • 10:11 - 10:16
    Here we can get watermelon
    and muskmelon added just
  • 10:16 - 10:18
    after the list.
  • 10:18 - 10:22
    Just add the last
    elements of the list.
  • 10:22 - 10:24
    Now if you want to delete
    a particular element from
  • 10:24 - 10:30
    the list, you can delete it
    with the syntax D=E=l space,
  • 10:30 - 10:33
    the list name you want
    to delete the element.
  • 10:33 - 10:36
    And in square brackets,
    you will write the index
  • 10:36 - 10:39
    for the element
    you want to delete.
  • 10:39 - 10:41
    We have written the
    fifth index value.
  • 10:41 - 10:45
    So we'll run this command and
    we'll get the list printed.
  • 10:45 - 10:50
  • 10:50 - 10:54
    We can see that the fifth index
    value, that is watermelon,
  • 10:54 - 10:55
    is not there in list1.
  • 10:55 - 10:58
    It's been deleted.
  • 10:58 - 11:02
    Now you can also do this with
    another method that is remove.
  • 11:02 - 11:06
    For that, we will
    write list1.remove
  • 11:06 - 11:10
    And in round brackets, we will
    write the particular element
  • 11:10 - 11:11
    we want to remove.
  • 11:11 - 11:15
    We will write
    that, that is kiwi.
  • 11:15 - 11:17
    And we also want the
    list to be printed.
  • 11:17 - 11:21
    So we'll write print in
    round brackets list1.
  • 11:21 - 11:25
    When we run this command,
    we get that the kiwi is
  • 11:25 - 11:28
    being removed from the list1.
  • 11:28 - 11:31
    Now there is another
    method that is pop.
  • 11:31 - 11:35
    We will write that
    command print.
  • 11:35 - 11:38
    In round brackets, we
    will write list1.pop.
  • 11:38 - 11:42
  • 11:42 - 11:45
    And we'll write the
    index value, what we
  • 11:45 - 11:47
    want to remove from the list.
  • 11:47 - 11:48
    That is 1.
  • 11:48 - 11:51
    And we also want the
    list to be printed.
  • 11:51 - 11:57
    So for that, we'll write
    the command print list1.
  • 11:57 - 11:58
    And we'll run this command.
  • 11:58 - 12:02
  • 12:02 - 12:06
    We have not written the
    name of the list correctly.
  • 12:06 - 12:08
    So now list1.
  • 12:08 - 12:12
    Now so at the one
    index that was orange
  • 12:12 - 12:14
    that has been removed
    from the list.
  • 12:14 - 12:16
    And now this is the new list.
  • 12:16 - 12:19
  • 12:19 - 12:22
    Now we can see another
    method that is clear,
  • 12:22 - 12:25
    that removes all the
    elements from the list.
  • 12:25 - 12:31
    So this, we would apply on
    the list2, list2.clear method.
  • 12:31 - 12:34
  • 12:34 - 12:36
    And we'll also write
    another command
  • 12:36 - 12:41
    that would be print list2
    and execute this command.
  • 12:41 - 12:43
    We can see that the
    list2 is empty now.
  • 12:43 - 12:46
    All the elements from the
    list2 have been removed.
  • 12:46 - 12:48
    Now we will see another
    method that is reverse.
  • 12:48 - 12:53
    For that, we will write the
    command list1.reverse and round
  • 12:53 - 12:54
    brackets.
  • 12:54 - 12:57
    And we also want the
    list1 to be printed.
  • 12:57 - 13:01
    For that, we will write
    the command print list1
  • 13:01 - 13:04
    and execute this command.
  • 13:04 - 13:07
    Here we can see that the
    list1 is being reversed.
  • 13:07 - 13:12
    Muskmelon, guava, banana, apple.
  • 13:12 - 13:16
    Now, if you want a particular
    index element to be accessed,
  • 13:16 - 13:19
    you can have the syntax.
  • 13:19 - 13:24
    For that, you can write
    that print list1.index.
  • 13:24 - 13:28
  • 13:28 - 13:30
    And in round brackets,
    you just need
  • 13:30 - 13:34
    to write the element for what
    you want to get the index
  • 13:34 - 13:36
    and just run this command.
  • 13:36 - 13:42
    You will get the index of
    the element guava that is 1.
  • 13:42 - 13:45
    We have another method
    in list that is count.
  • 13:45 - 13:48
    So count is used to
    give the number of times
  • 13:48 - 13:50
    the particular element
    is present in the list.
  • 13:50 - 13:55
    So for that, we will
    write the syntax print--
  • 13:55 - 13:57
    sorry, print.
  • 13:57 - 13:59
    And in round
    brackets, we'll write
  • 13:59 - 14:02
    the list name, list1.count.
  • 14:02 - 14:05
    In round brackets, we'll
    write the particular element
  • 14:05 - 14:08
    for which we want to get
    the number of times that
  • 14:08 - 14:10
    has been present in the list.
  • 14:10 - 14:16
    We'll write muskmelon
    and run this command.
  • 14:16 - 14:19
    It would give us the
    value 1, because it's
  • 14:19 - 14:22
    been present in the list
    for only a single time.
  • 14:22 - 14:24
    Now let's move to the tuples.
  • 14:24 - 14:25
    So tuple.
  • 14:25 - 14:27
    Tuple is almost same as list.
  • 14:27 - 14:30
    We can have different
    types of values in tuples.
  • 14:30 - 14:34
    The difference in list
    is we can change value
  • 14:34 - 14:38
    because list is mutable
    and tuple is immutable.
  • 14:38 - 14:41
    That means you cannot
    change the value.
  • 14:41 - 14:44
    A tuple can be defined as a
    collection of objects, values,
  • 14:44 - 14:46
    or items of different
    types, and these collections
  • 14:46 - 14:48
    are enclosed within
    the circle brackets
  • 14:48 - 14:50
    and separated by commas.
  • 14:50 - 14:53
    Let's understand this with
    the help of an example.
  • 14:53 - 14:56
    We will create a tuple
    and name it as tup1.
  • 14:56 - 14:58
    And insert the elements
    in round brackets
  • 14:58 - 15:00
    and separate them with
    the help of commas.
  • 15:00 - 15:03
    So tup1 equal to round brackets.
  • 15:03 - 15:07
    The first element is 1 comma
    and the second element is Adam,
  • 15:07 - 15:11
    third element is 107, and
    the fourth element is USA.
  • 15:11 - 15:15
    Now in tuples, we have our
    first element at zeroth index,
  • 15:15 - 15:19
    and we have 1 at zero
    index and Adam at one index
  • 15:19 - 15:23
    and 107 at second index
    and USA at third index.
  • 15:23 - 15:27
    And for the reverse index, it
    starts with minus 1 index value,
  • 15:27 - 15:32
    we have USA at minus 1 value,
    107 at minus 2 index value,
  • 15:32 - 15:37
    and Adam at minus 3 index value
    and 1 at minus 4 index value.
  • 15:37 - 15:39
    We will explore the tuples
    with various examples
  • 15:39 - 15:40
    in Jupyter Notebook.
  • 15:40 - 15:45
    But till then, let's look at
    the characteristics of tuple.
  • 15:45 - 15:48
    So the first is the
    tuples are ordered.
  • 15:48 - 15:49
    When we say that
    tuples are ordered,
  • 15:49 - 15:52
    it means that the items
    have a defined order
  • 15:52 - 15:54
    and that order will not change.
  • 15:54 - 15:57
    Elements of the tuples
    can be accessed by index.
  • 15:57 - 15:58
    Tuple items are indexed.
  • 15:58 - 16:04
    The first item has index
    0, the second index has 1.
  • 16:04 - 16:06
    Third, tuples can
    store various type
  • 16:06 - 16:12
    of elements, that is, strings,
    arrays, and many other elements.
  • 16:12 - 16:14
    The next is tuples
    are immutable.
  • 16:14 - 16:17
    Tuples are unchangeable, meaning
    that we cannot change, add,
  • 16:17 - 16:21
    or remove items after the
    tuple has been created.
  • 16:21 - 16:23
    Next is tuples allow
    duplicate elements.
  • 16:23 - 16:26
    Since tuples are
    indexed, they can
  • 16:26 - 16:28
    have items with the same value.
  • 16:28 - 16:32
    Let's have a look at some
    methods of tuples in Python.
  • 16:32 - 16:33
    First, that is index.
  • 16:33 - 16:36
    We can use the index
    operator to access
  • 16:36 - 16:39
    an item in a tuple with
    the index starts from 0.
  • 16:39 - 16:43
    Its index is the name
    of the tuple dot index.
  • 16:43 - 16:45
    And in round brackets, we'll
    write the element for which
  • 16:45 - 16:47
    we want to access the index.
  • 16:47 - 16:48
    The second is slicing.
  • 16:48 - 16:50
    We can access a range
    of items in a tuple
  • 16:50 - 16:52
    by using the slicing operator.
  • 16:52 - 16:56
    For that, we'll write the syntax
    tup1 and the range for which we
  • 16:56 - 16:59
    want to slice the tuple.
  • 16:59 - 17:00
    The third is concatenation.
  • 17:00 - 17:02
    Here we will add two tuples.
  • 17:02 - 17:05
    That is, we just have to
    write the name of the tuples
  • 17:05 - 17:06
    to add them.
  • 17:06 - 17:10
    And we will place the plus
    operator just between them.
  • 17:10 - 17:11
    The next is repetition.
  • 17:11 - 17:15
    We can have the same item
    multiple times in a tuple.
  • 17:15 - 17:17
    For that, we will
    just have to write
  • 17:17 - 17:19
    the syntax, the
    name of the tuple,
  • 17:19 - 17:23
    and the multiplication
    operator into the times we
  • 17:23 - 17:28
    want that value to be
    present in the tuple.
  • 17:28 - 17:29
    The next is count.
  • 17:29 - 17:31
    It returns the number
    of items a specified
  • 17:31 - 17:34
    value occurs in a tuple.
  • 17:34 - 17:37
    So for that, we have
    the syntax tup1.count.
  • 17:37 - 17:38
    And in round
    brackets, we will just
  • 17:38 - 17:40
    write the element
    for which we want
  • 17:40 - 17:43
    to count the number of times
    that specified value has
  • 17:43 - 17:44
    been occurred.
  • 17:44 - 17:47
    We also have another
    method that is index.
  • 17:47 - 17:49
    It searches the tuple
    for a specified value
  • 17:49 - 17:54
    and returns the position
    where it was found.
  • 17:54 - 17:55
    Now let's move to
    Jupyter Notebook
  • 17:55 - 17:57
    to explore these methods.
  • 17:57 - 17:58
    So let's start.
  • 17:58 - 18:02
    First, we will comment
    that would be tuples.
  • 18:02 - 18:06
    And we will move
    to the next column.
  • 18:06 - 18:08
    And first, we will
    create a tuple.
  • 18:08 - 18:12
    For that, we'll write
    the command tup1, that
  • 18:12 - 18:13
    would be the name of the tuple.
  • 18:13 - 18:18
    And in round brackets, we
    will insert the elements,
  • 18:18 - 18:36
    that would be apple comma,
    orange comma, banana comma,
  • 18:36 - 18:36
    kiwi.
  • 18:36 - 18:39
  • 18:39 - 18:43
    Now we will print this tuple.
  • 18:43 - 18:46
    We will just have to write
    print, and in round brackets,
  • 18:46 - 18:49
    the name of the tuple
    and run the command.
  • 18:49 - 18:53
    We will get the tuple printed.
  • 18:53 - 18:58
    Now if you just want to insert
    a single element in a tuple,
  • 18:58 - 19:02
    for that, we will write
    the command tup2 equal to,
  • 19:02 - 19:04
    in round brackets,
    you will write
  • 19:04 - 19:11
    a single element that would
    be apple and run that command.
  • 19:11 - 19:13
    The tuple is being
    created, but this tuple
  • 19:13 - 19:16
    is not being considered
    as tuple in Python.
  • 19:16 - 19:17
    It has been
    considered as string.
  • 19:17 - 19:21
    So let's check the
    type of this tuple.
  • 19:21 - 19:25
    For that, we will
    write print type.
  • 19:25 - 19:28
    And in round brackets, we'll
    write the name of the tuple.
  • 19:28 - 19:31
    We want to check and
    run this command.
  • 19:31 - 19:34
    Here we can see that
    it is showing a string.
  • 19:34 - 19:40
    So a single element is being
    shown as a string in Python.
  • 19:40 - 19:43
    If you want that
    it should be tuple,
  • 19:43 - 19:47
    for that, you just have
    to insert a single comma
  • 19:47 - 19:49
    in the tuple.
  • 19:49 - 19:54
    And now run this command, you
    will get this type as tuple.
  • 19:54 - 19:57
    Now if you want to access any
    element in a tuple, for that,
  • 19:57 - 20:03
    you just have to write
    the command print tup1.
  • 20:03 - 20:08
    And in square brackets,
    the index value that is 0.
  • 20:08 - 20:10
    And here you got the
    value apple that is
  • 20:10 - 20:14
    at zeroth index value in tup1.
  • 20:14 - 20:18
    Now we can also access the
    element in the reverse order.
  • 20:18 - 20:24
    For that, we will write
    the command print tup1.
  • 20:24 - 20:29
    And in square brackets, we will
    write the index value minus 1
  • 20:29 - 20:30
    and run the command.
  • 20:30 - 20:36
    Here we can get the value kiwi
    from the reverse index order.
  • 20:36 - 20:41
    Now we also have the slicing
    operator in tuples also.
  • 20:41 - 20:46
    For that, we will write
    the command print tup1.
  • 20:46 - 20:50
    in square brackets,
    the index values
  • 20:50 - 21:01
    where we will run this command
    print tup1, index value 1,
  • 21:01 - 21:03
    slicing operator 3.
  • 21:03 - 21:06
    So it has printed
    orange and banana.
  • 21:06 - 21:07
    So what does it means?
  • 21:07 - 21:12
    It prints the index
    value 1 and 2 and not 3.
  • 21:12 - 21:15
    We also have a repetition
    method in tuples.
  • 21:15 - 21:21
    For that, we just have to write
    a simple syntax like print
  • 21:21 - 21:24
    in square brackets, the
    element you want to repeat,
  • 21:24 - 21:30
    we'll write the element
    as repeat only and comma
  • 21:30 - 21:32
    to just consider it as tuple.
  • 21:32 - 21:35
    And just after the
    bracket, we would
  • 21:35 - 21:37
    have to write the
    multiply operator
  • 21:37 - 21:38
    and the number of
    times the element
  • 21:38 - 21:40
    should be repeated in the tuple.
  • 21:40 - 21:42
    Now we will run this command.
  • 21:42 - 21:45
    You can see that the tuple has
    been created with the element
  • 21:45 - 21:47
    repeat, and the
    number of times is 3
  • 21:47 - 21:51
    that we have written
    just after the element.
  • 21:51 - 21:53
    So the tuple has been created.
  • 21:53 - 21:55
    Now we will see
    the count method.
  • 21:55 - 21:59
    For that, we will write
    the command print the name
  • 21:59 - 22:03
    of the tuple dot count.
  • 22:03 - 22:06
    And in round brackets,
    we will write the element
  • 22:06 - 22:09
    for which we want to
    know the number of times
  • 22:09 - 22:11
    it been present in the tuple.
  • 22:11 - 22:13
    And we will execute the command.
  • 22:13 - 22:15
    It gives us 0.
  • 22:15 - 22:21
    As our tup1 does not
    consist guava as an element,
  • 22:21 - 22:23
    so it gives us the count as 0.
  • 22:23 - 22:27
    Now we'll see another
    method that would be index.
  • 22:27 - 22:33
    For that, we'll write the
    command print tup1.index.
  • 22:33 - 22:35
  • 22:35 - 22:37
    And in round
    brackets, we'll write
  • 22:37 - 22:42
    the element for which we want to
    fetch the index of that element.
  • 22:42 - 22:43
    Banana.
  • 22:43 - 22:45
    And we'll run this command.
  • 22:45 - 22:47
    We will get the index
    of the element banana
  • 22:47 - 22:54
    that is 2, 0, 1, 2.
  • 22:54 - 22:58
    So we get the index
    of the element banana.
  • 22:58 - 23:01
    Now if we want to use the
    append method in tuples,
  • 23:01 - 23:04
    we have to first create
    the tuple into the list.
  • 23:04 - 23:08
    For that, we will write the
    Command y equal to list.
  • 23:08 - 23:11
    And in round brackets,
    we will write
  • 23:11 - 23:17
    the name of the tuple which we
    want to convert in the list.
  • 23:17 - 23:19
    Now we will use
    the append method
  • 23:19 - 23:25
    to the list, that is
    y.append and we will insert
  • 23:25 - 23:28
    an element that would be guava.
  • 23:28 - 23:31
  • 23:31 - 23:47
    And now we would just assign
    the y list to a variable tup1.
  • 23:47 - 23:50
  • 23:50 - 23:56
    We have now converted the y list
    to the tuple with this syntax
  • 23:56 - 24:02
    that is tuple y, and we have
    assigned it to the tup1 value.
  • 24:02 - 24:09
    Now we will print this tuple
    and execute the command.
  • 24:09 - 24:12
    Here we can see
    that we have used
  • 24:12 - 24:15
    the append method in the tuple.
  • 24:15 - 24:19
    First, we have changed the tuple
    to the list by this command
  • 24:19 - 24:21
    y equal to list tup1.
  • 24:21 - 24:25
    Then we have used the append
    method y.append and appended
  • 24:25 - 24:26
    the element guava.
  • 24:26 - 24:31
    Then we have changed the y list
    to the tuple that is tup1 equal
  • 24:31 - 24:32
    to tuple, the name of the list.
  • 24:32 - 24:34
    And then we have
    printed the tuple.
  • 24:34 - 24:37
  • 24:37 - 24:40
    We can also add two
    tuples by another method.
  • 24:40 - 24:46
    For that, we will write
    another tuple that
  • 24:46 - 24:50
    will be tup3 equal to,
    we will insert an element
  • 24:50 - 24:51
    that would be cherry.
  • 24:51 - 24:54
  • 24:54 - 24:58
    And now we want it
    to be added in tup1.
  • 24:58 - 25:07
    So we will write
    tup1 equal to tup3.
  • 25:07 - 25:14
    And now we will print the
    final tuple that would be tup1.
  • 25:14 - 25:15
    And run the command.
  • 25:15 - 25:18
    Here we can see
    that we have added
  • 25:18 - 25:21
    two tuples, that is
    tuple1 and tuple3,
  • 25:21 - 25:23
    which consists of cherry.
  • 25:23 - 25:27
    And here we get the
    whole tuple as tuple1.
  • 25:27 - 25:32
    Now to execute loops in tuples,
    we will write the command.
  • 25:32 - 25:38
    For that, we will write the
    syntax for space a variable x
  • 25:38 - 25:44
    and the name of the tuple colon.
  • 25:44 - 25:50
    And then we will print the
    particular element one by one.
  • 25:50 - 25:51
    And we will run this command.
  • 25:51 - 25:54
    Here we can see that
    all the elements have
  • 25:54 - 25:58
    been printed from the tuple.
  • 25:58 - 26:02
    Now let's understand
    dictionaries in Python.
  • 26:02 - 26:04
    In lists, you can
    fetch the elements
  • 26:04 - 26:05
    with the help of index numbers.
  • 26:05 - 26:09
    But if you want to specify a
    different type of index for it,
  • 26:09 - 26:12
    example, if you have
    a list of eight values
  • 26:12 - 26:15
    and you want to fetch the fifth
    one, you will use the index.
  • 26:15 - 26:18
    But if you have key
    for each element,
  • 26:18 - 26:21
    then you can use the key
    to access that element.
  • 26:21 - 26:22
    Example, phonebook.
  • 26:22 - 26:25
    If you want to fetch a
    number, you will use a name
  • 26:25 - 26:26
    and you get a number.
  • 26:26 - 26:29
    And officially, if we
    talk about dictionary,
  • 26:29 - 26:31
    if you want to understand
    the meaning of a word,
  • 26:31 - 26:34
    you go to that page and
    you look at the word
  • 26:34 - 26:36
    and understand
    the meaning of it.
  • 26:36 - 26:39
    So this type of concept where
    you have a key and corresponding
  • 26:39 - 26:41
    to that, you have a value.
  • 26:41 - 26:45
    We can achieve this with
    the help of dictionaries.
  • 26:45 - 26:48
    Python dictionary can be defined
    as a collection of objects,
  • 26:48 - 26:51
    values, or items of different
    types stored in key value pair
  • 26:51 - 26:51
    format.
  • 26:51 - 26:54
    These multiple key
    value pairs created
  • 26:54 - 26:56
    are enclosed within
    the curly braces,
  • 26:56 - 26:59
    and each key is separated
    from its value by the colon.
  • 26:59 - 27:02
    Let's understand this with
    the help of an example.
  • 27:02 - 27:05
    Dict1 equal to curly braces.
  • 27:05 - 27:06
    The first key is a.
  • 27:06 - 27:09
    And corresponding to
    that, we have value
  • 27:09 - 27:10
    and that is separated by colon.
  • 27:10 - 27:14
    And after the key value pair
    that is separated by a comma,
  • 27:14 - 27:19
    the next is b key and that
    has a corresponding value 2.
  • 27:19 - 27:22
    After that, we have
    inserted a comma
  • 27:22 - 27:24
    to separate it
    from other values.
  • 27:24 - 27:27
    Now we have a third key that is
    C. And corresponding to that,
  • 27:27 - 27:28
    we have a value 3.
  • 27:28 - 27:32
    Let us see some characteristics
    of dictionaries.
  • 27:32 - 27:36
    The dictionaries are ordered
    from Python version 3.7.
  • 27:36 - 27:38
    When we say that
    dictionaries are ordered,
  • 27:38 - 27:40
    it means that the items
    have a defined order
  • 27:40 - 27:43
    and that order will not change.
  • 27:43 - 27:46
    Second, that is, elements of the
    dictionaries cannot be accessed
  • 27:46 - 27:47
    by index.
  • 27:47 - 27:50
    Third, dictionaries can store
    various types of elements.
  • 27:50 - 27:52
    Fourth, dictionaries
    are mutable.
  • 27:52 - 27:54
    Dictionaries are
    changeable, meaning
  • 27:54 - 27:57
    that we can change,
    add, or remove items
  • 27:57 - 27:59
    after the dictionary
    has been created.
  • 27:59 - 28:01
    Fifth one is
    dictionaries doesn't
  • 28:01 - 28:02
    allow duplicate elements.
  • 28:02 - 28:05
    Dictionaries cannot have
    two items with the same key.
  • 28:05 - 28:08
    Now let's see some
    methods in dictionaries.
  • 28:08 - 28:11
    The first is clear method.
  • 28:11 - 28:13
    It removes all the elements
    from the dictionary
  • 28:13 - 28:17
    and its syntax is the name
    of the dictionary dot clear.
  • 28:17 - 28:19
    The second is get.
  • 28:19 - 28:22
    It returns the value
    of the specified key.
  • 28:22 - 28:24
    The syntax is dict1.key.
  • 28:24 - 28:27
    And in square brackets, we
    have to write the key name.
  • 28:27 - 28:29
    The third is keys.
  • 28:29 - 28:32
    It returns a list containing
    the dictionary's key.
  • 28:32 - 28:34
    Its syntax is dict1.keys.
  • 28:34 - 28:36
    The next is pop.
  • 28:36 - 28:38
    It removes the element
    with the specified key.
  • 28:38 - 28:41
    Its syntax is dict1.pop.
  • 28:41 - 28:45
    And in round brackets, we would
    write the particular key name.
  • 28:45 - 28:47
    And the next method is pop item.
  • 28:47 - 28:51
    It removes the last inserted key
    value pair from the dictionary
  • 28:51 - 28:54
    and its syntax is dict1.popitem.
  • 28:54 - 28:56
    Let's move to the
    Jupyter Notebook
  • 28:56 - 28:59
    and execute these methods.
  • 28:59 - 29:03
    First, we will add a comment
    that would be dictionaries.
  • 29:03 - 29:08
    And now we'll
    create a dictionary.
  • 29:08 - 29:13
    For that, we'll write the
    syntax name of the dictionary
  • 29:13 - 29:17
    equal to curly braces.
  • 29:17 - 29:22
    First, we'll write the key
    value, then colon, and the value
  • 29:22 - 29:24
    corresponding to it.
  • 29:24 - 29:29
    We'll write apple, then
    separated by comma.
  • 29:29 - 29:35
    And then the second key
    value colon, and the value
  • 29:35 - 29:40
    corresponding to that
    key that would be orange.
  • 29:40 - 29:41
    And it will be
    separated by comma,
  • 29:41 - 29:46
    then we'll assign another key
    value that would be 3 colon.
  • 29:46 - 29:54
    And the value corresponding
    to that would be banana comma.
  • 29:54 - 29:58
    Next, the key value
    would be 4 colon.
  • 29:58 - 30:02
    And in inverted commas, we'll
    write the value corresponding
  • 30:02 - 30:04
    to it.
  • 30:04 - 30:06
    And we'll print this dictionary.
  • 30:06 - 30:09
  • 30:09 - 30:12
    Print dict1 and
    execute this command.
  • 30:12 - 30:14
    Here we can see that
    the dictionary is
  • 30:14 - 30:18
    printed with one key value, we
    have the corresponding value
  • 30:18 - 30:19
    apple.
  • 30:19 - 30:21
    With two key value, we have
    the corresponding value orange.
  • 30:21 - 30:23
    With three key value, we have
    the corresponding value banana.
  • 30:23 - 30:26
    And with four key value, we have
    the corresponding value kiwi.
  • 30:26 - 30:31
    Now to access a
    particular element,
  • 30:31 - 30:34
    we can't do that in
    dictionary, but we
  • 30:34 - 30:37
    can access a particular
    value with the help of key.
  • 30:37 - 30:38
    We can do that in dictionary.
  • 30:38 - 30:42
    For that, we would
    write the syntax print,
  • 30:42 - 30:44
    the name of the dictionary.
  • 30:44 - 30:47
    And in square
    brackets, we'll write
  • 30:47 - 30:50
    the key value that would be 1.
  • 30:50 - 30:52
    Run the command, we would
    get the value corresponding
  • 30:52 - 30:55
    to the key value
    that is apple that
  • 30:55 - 30:58
    is corresponding
    to the key value 1.
  • 30:58 - 31:02
    Now we will see a get
    method in dictionary.
  • 31:02 - 31:06
    For that, we'll write
    the command print
  • 31:06 - 31:09
    name of the dictionary dot get.
  • 31:09 - 31:13
    And in round brackets, we'll
    just write the key value
  • 31:13 - 31:15
    and run this command.
  • 31:15 - 31:19
    We'll get the corresponding
    value to the key value.
  • 31:19 - 31:20
    That is apple.
  • 31:20 - 31:24
    Now we will use another
    method that would be pop.
  • 31:24 - 31:29
    For that, we'll write
    the command print dict1.
  • 31:29 - 31:30
    Pop.
  • 31:30 - 31:34
    And in round brackets,
    we'll write the key value
  • 31:34 - 31:38
    for the value we want
    the element to be
  • 31:38 - 31:40
    removed from the dictionary.
  • 31:40 - 31:42
    We'll execute this command.
  • 31:42 - 31:44
    And we get kiwi as the output.
  • 31:44 - 31:49
    Now if we print the
    dictionary, print dict1,
  • 31:49 - 31:53
    we can see that the fourth
    key value and the value
  • 31:53 - 31:55
    corresponding to
    that key value has
  • 31:55 - 31:58
    been removed by the pop method.
  • 31:58 - 32:02
    Now we can also print the
    length of the dictionary.
  • 32:02 - 32:08
    For that, we'll write
    the command print len.
  • 32:08 - 32:12
    And in round brackets, we'll
    write the name of the dictionary
  • 32:12 - 32:14
    and just execute the command.
  • 32:14 - 32:17
    We will get 3 as
    the output as it
  • 32:17 - 32:20
    has been considered one unit,
    second unit, and third unit.
  • 32:20 - 32:22
    So the length is 3.
  • 32:22 - 32:26
    And we have another
    method that is sorted.
  • 32:26 - 32:32
    For that to write the command
    print sorted and the name
  • 32:32 - 32:35
    of the dictionary dict1.
  • 32:35 - 32:38
    And we'll execute this command.
  • 32:38 - 32:42
    It would give us all the key
    values in a sorted manner
  • 32:42 - 32:45
    when executed with this command.
  • 32:45 - 32:48
    Now we can see another
    method that is keys.
  • 32:48 - 32:52
    For that, we have to write the
    command print round brackets,
  • 32:52 - 32:57
    name of the dictionary
    dot keys, round brackets.
  • 32:57 - 32:59
    And we'll run this
    command and we'll
  • 32:59 - 33:03
    get all the keys that are
    present in dictionary.
  • 33:03 - 33:05
    There is another
    method that is values.
  • 33:05 - 33:14
    For that, we will write the
    command print dict1.values round
  • 33:14 - 33:15
    brackets.
  • 33:15 - 33:18
    And we will execute
    this command.
  • 33:18 - 33:21
    We will get all the values that
    are present in the dictionary.
  • 33:21 - 33:22
    Now we will see
    another method, that
  • 33:22 - 33:26
    is update that is used to
    update the particular value
  • 33:26 - 33:29
    corresponding to the
    key value or update
  • 33:29 - 33:32
    the dictionary with more
    values to be inserted
  • 33:32 - 33:33
    in the dictionary.
  • 33:33 - 33:35
    For that, we'll write
    the command dict1.update.
  • 33:35 - 33:40
  • 33:40 - 33:45
    In round brackets, first, we
    will write the curly braces,
  • 33:45 - 33:49
    the key value semicolon colon.
  • 33:49 - 33:51
    And corresponding to that,
    we will write its value,
  • 33:51 - 33:54
    which we want to update.
  • 33:54 - 33:56
    Watermelon.
  • 33:56 - 34:03
    And we'll also print
    the dictionary.
  • 34:03 - 34:05
    We will execute this command.
  • 34:05 - 34:09
    We will get that the second
    value that is watermelon,
  • 34:09 - 34:11
    that's been updated.
  • 34:11 - 34:15
    Previously, it was orange
    and now it's watermelon.
  • 34:15 - 34:17
    So it's been updated
    by this command.
  • 34:17 - 34:20
    And we can also use this
    command to add more elements
  • 34:20 - 34:22
    to the dictionary.
  • 34:22 - 34:29
    For that, we'll write the
    syntax dict1.update round
  • 34:29 - 34:34
    brackets, curly braces,
    and the key value colon.
  • 34:34 - 34:41
    And corresponding to that, we
    will write the value muskmelon
  • 34:41 - 34:48
    and we'll print the dictionary
    and we'll execute this command.
  • 34:48 - 34:53
  • 34:53 - 34:59
    Sorry, we just omitted the name
    of the dictionary, dict1.update.
  • 34:59 - 35:02
    Now we'll execute this command.
  • 35:02 - 35:04
    Here we can see
    that the value has
  • 35:04 - 35:06
    been added to the dictionary.
  • 35:06 - 35:08
  • 35:08 - 35:10
    Now we'll see another
    method that is pop.
  • 35:10 - 35:15
    For that, we'll write
    the command dict1.pop.
  • 35:15 - 35:18
    And in round brackets,
    the key value
  • 35:18 - 35:23
    for which we want the value to
    be removed from the dictionary.
  • 35:23 - 35:28
    And we will print the dictionary
    and execute this command.
  • 35:28 - 35:31
    Here we can see that the
    second key value has been
  • 35:31 - 35:34
    removed from the dictionary.
  • 35:34 - 35:37
    And we will see another method
    that would be dict1.popitem.
  • 35:37 - 35:42
  • 35:42 - 35:46
    And then we will
    print the dictionary.
  • 35:46 - 35:49
  • 35:49 - 35:51
    And execute the command.
  • 35:51 - 35:53
    Here we can see that
    the pop item has removed
  • 35:53 - 35:55
    the last element
    from the dictionary,
  • 35:55 - 35:59
    and we are left with only two
    elements in the dictionary.
  • 35:59 - 36:02
    Now we will see some
    loops in the dictionary.
  • 36:02 - 36:04
    For that, we'll
    write the command
  • 36:04 - 36:12
    for x in the name of the
    dictionary, that is dict1 colon
  • 36:12 - 36:18
    and then we'll
    write print dict1.
  • 36:18 - 36:21
    And in square brackets,
    the variable x.
  • 36:21 - 36:24
    And we will execute
    this command.
  • 36:24 - 36:28
    So with this loop, we can print
    all the values corresponding
  • 36:28 - 36:30
    to the key values
    in the dictionary.
  • 36:30 - 36:34
    Now to print both the keys and
    values corresponding to it,
  • 36:34 - 36:35
    we can run a loop.
  • 36:35 - 36:36
    For that, we'll
    write the command
  • 36:36 - 36:54
    for x comma y in dict1.items
    colon print x comma y.
  • 36:54 - 36:56
    And run this command.
  • 36:56 - 36:59
    We can get the key and the
    values corresponding to it.
  • 36:59 - 37:01
  • 37:01 - 37:04
    Now if you want to copy
    the particular dictionary
  • 37:04 - 37:08
    in another dictionary,
    you can write the command.
  • 37:08 - 37:10
    First, you will
    name the dictionary
  • 37:10 - 37:13
    you want to create
    and copy the elements.
  • 37:13 - 37:18
    We will name it as
    mydict equal to.
  • 37:18 - 37:20
    Now we'll write the
    command dict1.copy.
  • 37:20 - 37:24
  • 37:24 - 37:31
    And then we will print the new
    dictionary, that is mydict.
  • 37:31 - 37:34
  • 37:34 - 37:38
    And we'll run this command.
  • 37:38 - 37:42
    We have not inserted
    the round bracket here.
  • 37:42 - 37:43
    Now run this command.
  • 37:43 - 37:47
    Here we can see that we have
    printed the mydict dictionary.
  • 37:47 - 37:51
    And it has the same
    values as dict1.
  • 37:51 - 37:59
    Now we'll see another method
    to delete the whole dictionary.
  • 37:59 - 38:02
    For that, we'll
    write dict1.clear.
  • 38:02 - 38:06
  • 38:06 - 38:08
    It would remove all the
    values from the dictionary
  • 38:08 - 38:14
    and will also print the
    dictionary after that.
  • 38:14 - 38:15
    Run this command.
  • 38:15 - 38:17
    Here we can see all the
    elements from the dictionary
  • 38:17 - 38:19
    have been removed.
  • 38:19 - 38:22
    With that, we have come to
    the end of this session.
  • 38:22 - 38:24
    I hope it was interesting
    and informative.
  • 38:24 - 38:27
    If you liked it, please let
    us in the comment section.
  • 38:27 - 38:29
    Also do subscribe to our
    channel and stay tuned
  • 38:29 - 38:31
    for more from Simplilearn.
  • 38:31 - 38:35
  • 38:35 - 38:36
    Hi there.
  • 38:36 - 38:38
    If you like this video,
    subscribe to the Simplilearn
  • 38:38 - 38:42
    YouTube channel and click
    here to watch similar videos.
  • 38:42 - 38:45
    To nerd up and get
    certified, click here.
  • 38:45 - 38:46
Title:
Python Lists, Tuples And Dictionaries - 10 | Python For Beginners | Python Tutorial | Simplilearn
Description:

more » « less
Video Language:
English
Duration:
38:46

English subtitles

Revisions