How to Use Lists in Python

00:18:48
https://www.youtube.com/watch?v=9OeznAkyQz4

الملخص

TLDRThis tutorial introduces the basic data structures in Python, focusing mainly on lists, a versatile data type for managing collections of items. Lists can hold items of any type, including other lists, allowing the creation of complex data structures like matrices. The tutorial explores various operations with lists, such as creating lists using the range() and list() functions, accessing and modifying list elements, and using list slicing and unpacking for efficient data handling. The video demonstrates looping over lists with 'for' loops and the enumerate() function to retrieve both index and value of list items. It teaches how to add with append() and insert() methods, remove items using pop(), remove() methods, and the delete statement for a range of entries. Finally, it covers finding object indices within lists and using count() to tally item occurrences. The video concludes with an invitation to viewers to explore more Python tutorials.

الوجبات الجاهزة

  • 🔢 Lists can contain any data type, including other lists.
  • 🔄 Use operations like append() to add to lists and pop() to remove.
  • 🚀 Efficient indexing is key: use list[index] or negative indices for reverse order.
  • ✂️ Slice lists with list[start:end] for sublists.
  • 📜 Use enumerate() to get index and value during iteration.
  • 📈 Unpacking assigns list elements to multiple variables instantly.
  • 💡 Important list methods: append, insert, remove, pop, clear.
  • 📋 The in operator checks existence of elements in lists.
  • 🔍 Use try-except to handle errors when searching for non-existing items.
  • 🛠 Clear and pack remaining items with packing syntax using *.

الجدول الزمني

  • 00:00:00 - 00:05:00

    In the initial section, the discussion revolves around Python's built-in data structures necessary for real application development, starting with lists. Lists in Python can contain diverse data types, including numbers, strings, booleans, or even other lists (resulting in multi-dimensional lists). Techniques to create lists more efficiently are demonstrated using multiplication and the range function to avoid manual list entries. Concatenation of lists is achieved with a plus operator, showing the merging of different type lists. Briefly, list indexing, both positive and negative, for accessing elements, and slicing for subset extraction, are explained with examples.

  • 00:05:00 - 00:10:00

    The next part elaborates on list slicing techniques, including using steps to access elements intermittently within the list. Different slicing examples show how to create sublists based on a starting and ending index, or even the step, to skip elements, demonstrating how Python manages default index values when omitted. It also touches upon reversing a list using a negative step and introduces list unpacking. List unpacking is a convenient way to assign list items to multiple variables. The concept of packing the rest of the list into another one variable is shown to handle situations when the list size is uncertain. This part ties into earlier discussions on functions handling dynamic arguments using an asterisk.

  • 00:10:00 - 00:18:48

    The final segment describes looping through lists with a for loop, using 'enumerate' to iterate with index-value pairs. It emphasizes different methods to modify lists - adding elements via 'append' and 'insert,' or removing them with 'pop,' 'remove,' or 'del,' noting their different use-cases. Finding elements involves checking their presence with 'in' before using 'index' to prevent errors and using 'count' to determine occurrences. The tutorial is wrapped up with reference to additional resources for learning Python in depth. This closing addresses viewers directly, encouraging subscription and engagement with the content creator's broader educational offerings.

الخريطة الذهنية

Mind Map

الأسئلة الشائعة

  • What are the basic data structures covered in Python in this video?

    The video covers lists, tuples, sets, and dictionaries in Python.

  • How can you create a list of 100 zeros in Python?

    You can create a list of 100 zeros using the syntax [0] * 100.

  • How do you concatenate two lists in Python?

    You can concatenate two lists using the + operator.

  • How do you access every second element of a list in Python?

    You can access every second element of a list using slicing with a step, e.g., list[::2].

  • How do you reverse a list in Python?

    You reverse a list using slicing with a negative step, i.e., list[::-1].

  • What is list unpacking in Python?

    List unpacking is assigning elements of a list to multiple variables in a single statement.

  • How do you add a new item to the end of a list in Python?

    You can add a new item to the end of a list using the append() method.

  • What method removes the last item from a list in Python?

    The pop() method removes the last item from a list.

  • How can you find an index of an element in a list?

    You can find the index of an element using the index() method.

  • How do you safely get the index of an element in Python without causing an error?

    First, check if the element is in the list using 'in'; then use index() if it exists.

عرض المزيد من ملخصات الفيديو

احصل على وصول فوري إلى ملخصات فيديو YouTube المجانية المدعومة بالذكاء الاصطناعي!
الترجمات
en
التمرير التلقائي:
  • 00:00:02
    in this section we're going to look at
  • 00:00:05
    the built-in data structures in Python
  • 00:00:07
    which are extremely important when
  • 00:00:09
    building real applications first we're
  • 00:00:12
    going to look at lists and then we'll
  • 00:00:13
    look at tuples sets and dictionaries so
  • 00:00:17
    earlier you have seen that we can use
  • 00:00:18
    square brackets to define a list or a
  • 00:00:21
    sequence of objects in between these
  • 00:00:24
    brackets we can have objects of any type
  • 00:00:26
    so we can have a list of strings like
  • 00:00:29
    this and then assign it to a variable
  • 00:00:31
    like letters we can also have a list of
  • 00:00:36
    numbers boolean or even a list of lists
  • 00:00:38
    let me show you so here we have a list
  • 00:00:41
    each item in this list will be a list
  • 00:00:44
    itself so here's the first item which is
  • 00:00:48
    a list of two items now let's add
  • 00:00:51
    another item to our main or parent list
  • 00:00:54
    this item is also a list with two items
  • 00:00:57
    so now we have a matrix which is a two
  • 00:01:01
    dimensional list now let me show you
  • 00:01:04
    some cool tricks let's say you want to
  • 00:01:06
    have a list of a hundred zeros you don't
  • 00:01:09
    want to manually create that like this
  • 00:01:11
    that's very ugly let me show you a
  • 00:01:13
    better way so we define a list of one
  • 00:01:16
    item on 0 and then we can multiply it by
  • 00:01:19
    100 and the result will be this let me
  • 00:01:23
    show you friend zeros here it is there
  • 00:01:29
    you go so using a star or an asterisk we
  • 00:01:33
    can repeat the items in a list now
  • 00:01:35
    similarly we can use a plus to
  • 00:01:38
    concatenate multiple lists let me show
  • 00:01:40
    you so first I'm gonna change this to 5
  • 00:01:44
    now let's define a variable combined
  • 00:01:47
    which is our zeros list plus letters
  • 00:01:52
    let's see what happens print combined
  • 00:01:55
    you know it so we have 5 zeros followed
  • 00:01:59
    by ABC as you can see in Python every
  • 00:02:03
    object in a list can be of a different
  • 00:02:05
    type so they don't have to be exactly
  • 00:02:07
    the same type we can combine a list of
  • 00:02:09
    numbers with strings and boolean or even
  • 00:02:12
    lists
  • 00:02:13
    now let's say you want to have a list of
  • 00:02:15
    numbers like 0 1 2 3 all the way up to
  • 00:02:18
    20 you don't want to type all of these
  • 00:02:20
    by hand there is a better way so we have
  • 00:02:23
    this list function as you can see this
  • 00:02:26
    function takes an iterable
  • 00:02:27
    so we can pass any iterable here and
  • 00:02:30
    convert it to a list earlier you learn
  • 00:02:32
    about the range function this function
  • 00:02:35
    returns a range object which is iterable
  • 00:02:38
    which means we can iterate or loop over
  • 00:02:40
    it so here we can call this function and
  • 00:02:43
    pass 20 and with this we can create a
  • 00:02:46
    list of numbers from 0 to 20 let me show
  • 00:02:49
    you so let's store it in numbers and
  • 00:02:52
    then print it on the terminal there you
  • 00:02:57
    go so 0 up to 20 but note that 20 itself
  • 00:03:00
    is not included as another example let's
  • 00:03:04
    call the list function and pass a string
  • 00:03:08
    earlier I told you that strings are also
  • 00:03:11
    a turrible we can loop over them so we
  • 00:03:13
    can pass them to the list function and
  • 00:03:16
    see what we get let's print chars on the
  • 00:03:21
    terminal so you can see each character
  • 00:03:24
    in our original string is an item in
  • 00:03:27
    this list so these are a few different
  • 00:03:30
    ways to create a list in Python now that
  • 00:03:33
    we have a list we can get the number of
  • 00:03:35
    items in that list using the Len
  • 00:03:37
    function so here we can print the Len or
  • 00:03:40
    length of chars let's take a look so we
  • 00:03:44
    have 11 items in this list over the next
  • 00:03:48
    few lectures we'll look at various
  • 00:03:49
    operations around lists
  • 00:03:58
    so here we have a list of four items we
  • 00:04:02
    can use square brackets to access
  • 00:04:04
    individual items in this list so let's
  • 00:04:07
    print letters of zero this will return
  • 00:04:10
    the first item in this list so when we
  • 00:04:14
    run this program we'll get a now similar
  • 00:04:17
    to strings if we pass a negative index
  • 00:04:20
    here like negative one this will return
  • 00:04:23
    the first item from the end of the list
  • 00:04:26
    so when you run this we'll get D using
  • 00:04:31
    square brackets we can also modify items
  • 00:04:33
    in the list so let's change the first
  • 00:04:37
    item to a capital A and then print the
  • 00:04:40
    entire list there you go so this is the
  • 00:04:44
    basic of accessing individual elements
  • 00:04:47
    in the list
  • 00:04:48
    the earlier in the course you learn that
  • 00:04:50
    we can use two indexes to slice a string
  • 00:04:53
    we have the exact same concept here so
  • 00:04:56
    we add square brackets first index colon
  • 00:05:00
    second index and this will return a new
  • 00:05:04
    list with the first three items in our
  • 00:05:06
    original list so if we print our
  • 00:05:09
    original list
  • 00:05:12
    you can see that it's not changed now
  • 00:05:15
    just like strings if you don't specify
  • 00:05:17
    the first argument 0 will be assumed by
  • 00:05:20
    default so as you can see these two
  • 00:05:22
    expressions produce the exact same
  • 00:05:24
    result similarly if you don't include
  • 00:05:28
    the end index by default the lengths of
  • 00:05:31
    the list will be used here so this
  • 00:05:34
    expression will return a new list with
  • 00:05:36
    all the items in the original list and
  • 00:05:39
    similarly we can also exclude the start
  • 00:05:43
    index here and with this syntax we can
  • 00:05:45
    get a copy of our original list there
  • 00:05:48
    you go
  • 00:05:49
    now when slicing a string we can also
  • 00:05:52
    pass a step and this is useful in
  • 00:05:55
    situations where you want to return
  • 00:05:57
    every second or every third element in
  • 00:06:00
    the original list so now when we run
  • 00:06:03
    this code we'll get a and C so B will be
  • 00:06:07
    a skipped let me show you using a better
  • 00:06:10
    example so I'm going to delete
  • 00:06:13
    everything here create a new list called
  • 00:06:15
    numbers here we're gonna use the list
  • 00:06:18
    function and pass range of 20 let's
  • 00:06:23
    print our list so we get numbers 0 to 19
  • 00:06:28
    okay now let's see what happens when we
  • 00:06:31
    add square brackets here with two colons
  • 00:06:34
    and two this will return every other
  • 00:06:38
    element in the original list take a look
  • 00:06:41
    so we get all the even numbers 0 2 4 and
  • 00:06:45
    so on it's pretty cool isn't it here's
  • 00:06:48
    another cool thing you can do here let's
  • 00:06:50
    change the step to negative 1 as you can
  • 00:06:55
    see this will return all the items in
  • 00:06:57
    the original list but in reverse order
  • 00:07:00
    so these are some useful things you can
  • 00:07:03
    do with lists next we'll look at
  • 00:07:05
    unpacking lists
  • 00:07:13
    there are times that you may want to get
  • 00:07:16
    individual items in a list and assign
  • 00:07:18
    them to different variables here is an
  • 00:07:20
    example we can define a variable like
  • 00:07:22
    first and set it to numbers of zero
  • 00:07:25
    similarly we can define second set it to
  • 00:07:28
    numbers of one and third set it to
  • 00:07:31
    numbers of two perhaps we are going to
  • 00:07:34
    use these variables in a few complex
  • 00:07:37
    expressions in your code now there is a
  • 00:07:39
    cleaner and more elegant way to achieve
  • 00:07:42
    the same result and that is what we call
  • 00:07:45
    list unpacking so we can unpack this
  • 00:07:49
    list into multiple variables let me show
  • 00:07:52
    you how that works so we define our
  • 00:07:54
    variables like first second and third
  • 00:07:57
    and then set them to our list what we
  • 00:08:01
    have on line two is exactly identical to
  • 00:08:05
    what we have on lines four to six this
  • 00:08:07
    is what we call list unpacking now what
  • 00:08:11
    is important here is that the number of
  • 00:08:13
    variables that we have on the left side
  • 00:08:15
    of the assignment operator should be
  • 00:08:17
    equal to the number of items we have in
  • 00:08:19
    the list so if we exclude third here and
  • 00:08:23
    run this program
  • 00:08:25
    we will get an error value error too
  • 00:08:29
    many values to unpack so there are too
  • 00:08:31
    many items in this list and we cannot
  • 00:08:34
    unpack it into enough variables now what
  • 00:08:39
    if in this list we have so many items
  • 00:08:41
    but we only care about the first two we
  • 00:08:45
    don't want to define so many variables
  • 00:08:47
    on the left side of the assignment
  • 00:08:48
    operator well we can get the first and
  • 00:08:51
    second and then pack the rest inside of
  • 00:08:56
    a separate list called other with this
  • 00:08:59
    syntax we'll get the first and second
  • 00:09:02
    items and everything else will be stored
  • 00:09:04
    in a separate list called other let me
  • 00:09:07
    show you so let's print first and let's
  • 00:09:10
    also print other now we don't need these
  • 00:09:14
    few lines here let's run this code so
  • 00:09:17
    first is one and other is a list of all
  • 00:09:22
    the items after
  • 00:09:24
    the second item that is the list I'm
  • 00:09:26
    talking about so in this example we have
  • 00:09:29
    both unpacking and packing first we try
  • 00:09:33
    to unpack this numbers list into the
  • 00:09:36
    variables on the left side of the
  • 00:09:38
    assignment operator and then because we
  • 00:09:40
    have used an asterisk here we're
  • 00:09:42
    basically packing all the other items
  • 00:09:45
    into a separate list now to refresh your
  • 00:09:49
    memory
  • 00:09:49
    earlier we use this syntax when defining
  • 00:09:52
    a function with a variable number of
  • 00:09:54
    arguments remember we had a function
  • 00:09:57
    like this multiplied with a parameter
  • 00:10:01
    called asterisk numbers and then we
  • 00:10:04
    could call this multiplied with
  • 00:10:08
    arbitrary number of arguments so when we
  • 00:10:11
    prefix a parameter with an asterisk
  • 00:10:14
    Python will get all these arbitrary
  • 00:10:16
    arguments and pack them into a list this
  • 00:10:19
    is exactly what is happening on line two
  • 00:10:22
    now let me delete this other stuff now
  • 00:10:27
    let's change this example a little bit
  • 00:10:28
    what if we care only about the first and
  • 00:10:31
    the last item well we can put other in
  • 00:10:35
    between so we get the first other and
  • 00:10:38
    then the last item so let's change the
  • 00:10:42
    last item to nine and then print first
  • 00:10:47
    last and other this is what we get so
  • 00:10:52
    first there's one last is nine and the
  • 00:10:55
    rest is here so this is all about list
  • 00:10:58
    unpacking
  • 00:11:05
    in this lecture I'm gonna show you how
  • 00:11:08
    to loop over lists so here we have a
  • 00:11:10
    list of three items we can use our four
  • 00:11:13
    loops to loop over this list so for
  • 00:11:16
    letter in letters : and then we print
  • 00:11:21
    each letter save the changes and run the
  • 00:11:25
    code we get ABC now what if you want to
  • 00:11:28
    get the index of each item as well well
  • 00:11:31
    we have a built-in function called
  • 00:11:33
    enumerate we call it here and this will
  • 00:11:37
    return an enumerator object which is
  • 00:11:40
    iterable in each iteration this
  • 00:11:43
    enumerate object will give us a tapa let
  • 00:11:46
    me show you so now when we run this code
  • 00:11:48
    look in each iteration we're getting a
  • 00:11:51
    topple so a topple as I told you before
  • 00:11:54
    is like your list but it's read-only we
  • 00:11:56
    cannot add new items to it so in each
  • 00:11:59
    iteration we're getting a topple of two
  • 00:12:02
    items the first item in this topple is
  • 00:12:04
    the index and the second item is the
  • 00:12:08
    item at that index so now to get the
  • 00:12:10
    index we can use square brackets to
  • 00:12:13
    access the first item in this topple so
  • 00:12:16
    if we print letter of 0 we will get the
  • 00:12:21
    indexes and right next to that we can
  • 00:12:23
    add letter a 1 so we will see the item
  • 00:12:28
    at a given index but this syntax is a
  • 00:12:31
    little bit ugly in the last lecture
  • 00:12:33
    you'll learn about list unpacking so if
  • 00:12:36
    we have a list with two items 0 and a we
  • 00:12:42
    can unpack it into two variables like
  • 00:12:45
    this index comma letter equals items so
  • 00:12:50
    here we are unpacking the items list now
  • 00:12:54
    what if we change square brackets to
  • 00:12:56
    parentheses now we have a table all and
  • 00:12:59
    we can still unpack this topple so you
  • 00:13:02
    saw that this enumerate function returns
  • 00:13:05
    an enumerate object which is iterable in
  • 00:13:08
    each iteration this enumerate object
  • 00:13:11
    will return a tuple that looks like this
  • 00:13:14
    so we can unpack
  • 00:13:16
    kid right here so we add another
  • 00:13:20
    variable index now with this we no
  • 00:13:23
    longer have to use square brackets and
  • 00:13:25
    we can simply print index and letter
  • 00:13:28
    let's run this code there you go so now
  • 00:13:32
    we don't need this anymore
  • 00:13:35
    so to recap you can use four loops to
  • 00:13:39
    iterate over lists if you also need the
  • 00:13:42
    index you should call the enumerate
  • 00:13:44
    function this will return an enumerate
  • 00:13:46
    object which is iterable in each
  • 00:13:49
    iteration it will return a tuple and you
  • 00:13:52
    can unpack that topple right here
  • 00:14:01
    in this lecture I'm going to show you
  • 00:14:03
    how to add new items to a list or remove
  • 00:14:06
    existing items so for adding items you
  • 00:14:09
    have two options depending on where you
  • 00:14:12
    want to add this new item if you want to
  • 00:14:14
    add an item at the end of the list you
  • 00:14:16
    should use the append method so earlier
  • 00:14:19
    you learned that everything in Python is
  • 00:14:21
    an object so we can use the dot notation
  • 00:14:23
    to access individual functions or more
  • 00:14:27
    accurately methods in that object so
  • 00:14:30
    when a function is part of an object we
  • 00:14:32
    refer to that function as a method so
  • 00:14:34
    here are all the methods available on
  • 00:14:36
    list objects we use the append method to
  • 00:14:40
    add something at the end of this list
  • 00:14:42
    let's print our letters and we will get
  • 00:14:47
    ABCD beautiful now if you want to add an
  • 00:14:50
    item at a specific position you should
  • 00:14:53
    use the insert method so letters that
  • 00:14:56
    insert we can add something at the
  • 00:14:59
    beginning of the list so index 0 let's
  • 00:15:03
    add a hyphen and then print the result
  • 00:15:06
    so this is what we get now for removing
  • 00:15:10
    objects again you have a few different
  • 00:15:12
    options if you want to remove the item
  • 00:15:15
    at the end of the list you should use
  • 00:15:17
    the pop method so here recall letters
  • 00:15:20
    dot pop this will remove the letter D at
  • 00:15:23
    the end of our list
  • 00:15:25
    so now let's print our letters as you
  • 00:15:30
    can see D is gone we can also pass on
  • 00:15:32
    index here to remove the item at the
  • 00:15:35
    given index so if you pass 0 instead of
  • 00:15:39
    D this - will be removed let's take a
  • 00:15:42
    look we run this so the hyphen is gone
  • 00:15:45
    and we get ABCD beautiful now there are
  • 00:15:48
    times that you want to remove an object
  • 00:15:49
    but you don't know its index if that's
  • 00:15:52
    the case we can use the remove method so
  • 00:15:55
    letters that remove here we can remove B
  • 00:15:59
    and this will remove the first
  • 00:16:01
    occurrence of the letter B so if you
  • 00:16:04
    have multiple B's only the first one
  • 00:16:06
    will be removed if you want to remove
  • 00:16:08
    all B's in this list you'll have to loop
  • 00:16:10
    over this list and we
  • 00:16:11
    of each be individually now let's run
  • 00:16:14
    this code one more time so you can see B
  • 00:16:17
    is gone we have another way to remove an
  • 00:16:20
    item from a list and that is using the
  • 00:16:22
    Dell or delete statement so here we can
  • 00:16:25
    delete an item by its index we can also
  • 00:16:28
    delete a range of items so this is the
  • 00:16:31
    difference between the delete statement
  • 00:16:33
    and the pop method the pop method will
  • 00:16:36
    remove only one item by index whereas
  • 00:16:39
    with the delete statement we can remove
  • 00:16:41
    a range of items and finally if you want
  • 00:16:44
    to remove all the objects in the list
  • 00:16:46
    you should use the clear method next
  • 00:16:50
    we'll look at finding objects in a list
  • 00:16:59
    there are times that you want to find
  • 00:17:01
    the index of a given object in a list so
  • 00:17:04
    let's say we want to find the index of
  • 00:17:06
    letter A in our letters list we call
  • 00:17:09
    letters dot index and pass a let's print
  • 00:17:14
    the result so this will return 0 what if
  • 00:17:19
    you try to get the index of an object
  • 00:17:22
    that doesn't exist here like D we get a
  • 00:17:25
    value error D is not in the list this
  • 00:17:29
    behavior is different from a lot of
  • 00:17:31
    programming languages out there C based
  • 00:17:33
    languages return negative 1 if you try
  • 00:17:36
    to get the index of an object that
  • 00:17:38
    doesn't exist in the list but in Python
  • 00:17:40
    we get an error so to prevent this error
  • 00:17:44
    from happening first you should check to
  • 00:17:45
    see if the given object exists in the
  • 00:17:48
    list and for that we use the in operator
  • 00:17:51
    so if D is in letters then we will print
  • 00:17:56
    its index so now we run the program and
  • 00:18:00
    we don't get any errors we also have
  • 00:18:03
    another method that you might find
  • 00:18:04
    useful in certain situations and that is
  • 00:18:07
    count so letters that count this will
  • 00:18:11
    return the number of occurrences of the
  • 00:18:13
    given item in this list so when we print
  • 00:18:16
    the result we'll get 0
  • 00:18:23
    hi guys thank you for watching this
  • 00:18:25
    tutorial my name is Mohamad ani and I
  • 00:18:27
    have tons of tutorials like this for you
  • 00:18:29
    on my channel so be sure to subscribe
  • 00:18:31
    and also please like and share this
  • 00:18:34
    video if you want to learn Python
  • 00:18:35
    properly from scratch with depth I have
  • 00:18:38
    a comprehensive python tutorial for you
  • 00:18:40
    the link is below this video so click
  • 00:18:42
    the link to get started
  • 00:18:44
    thank you and have a fantastic day
الوسوم
  • Python
  • Lists
  • Data Structures
  • Lists Operations
  • Tuples
  • Sets
  • Dictionaries
  • Unpacking
  • List Slicing
  • Indexing