Guidelines

How to Utilize Values in a List

To utilize values within a list, you use an index that starts from 0.

The index of the first element is 0, the second element is 1, and it increases sequentially after that.

The index of the last element is -1, the second-to-last is -2, decreasing sequentially.

Example of Accessing Elements Using Index
fruits = ["Apple", "Banana", "Grape", "Cherry"] # First element first_fruit = fruits[0] # "Apple" print("first_fruit:", first_fruit) # Second element second_fruit = fruits[1] # "Banana" print("second_fruit:", second_fruit) # Last element last_fruit = fruits[-1] # "Cherry" print("last_fruit:", last_fruit)

Slicing to Retrieve a Portion of a List

To retrieve a portion of a list, we use slicing.

Slicing is a method that uses list indices to extract the desired portion.

To slice a list, you specify the start and end index separated by a colon : inside the square brackets [].

For example, you can slice the fruits list with fruits[0:2].

The start index is included, but the end index is not.

For example, fruits[0:2] extracts from the first element to the second element.

The third element, fruits[2], is not included in the result of the slice.

Example of List Slicing
fruits = ["Apple", "Banana", "Grape", "Cherry"] # From the first element to the second element first_two_fruits = fruits[0:2] # fruits[2] is not included print("first_two_fruits:", first_two_fruits) # ['Apple', 'Banana']

Nested Lists

A nested list refers to a list that includes other lists as its elements.

This allows you to represent complex data structures like multidimensional arrays or matrices.

Example of Nested Lists
# Create a 2D list nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # First element of the nested list print(nested_list[0]) # Output: [1, 2, 3] # Specific element of a nested list print(nested_list[0][1]) # Output: 2 (2nd element of the 1st list)
Mission
0 / 1

What is the correct index to fill in the blank?

To access the first element from the end of a list, set the index to .
0
1
2
-1

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result