![]() We then created an integer variable, half_length, which is the result of dividing the length of the list by two using integer division.Let’s break down what we did in the code above:Ī_list, which contains ten different items Let’s see how we can use list slicing to split a list in half: # Split a List in Half with Python List Slicingįirst_half, second_half = a_list, a_list As you learned above, you can select multiple items in a list using list slicing. You can easily split a Python list in half using list indexing. ![]() This can be done by using the : colon character, which allows you to select items from a specific position up to a specific position. A negative list index begins at -1, allowing you to retrieve the last item of a list.ĭata within a list using list slicing. ![]() A positive index begins at position 0, meaning the first item. We can see here that Python lists have both a positive index as well as a negative index. This means that we can access an item, or a range of items, by its index. One of the many wonderful properties of lists is that they are ordered.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
March 2023
Categories |