• Javascript
  • Python
  • Go
Tags: python list

Iterating a List with Indexes in Python

Python is a powerful and versatile programming language that is widely used in a variety of applications. One of the key features of Python ...

Python is a powerful and versatile programming language that is widely used in a variety of applications. One of the key features of Python is its ability to work with lists, which are a fundamental data structure in the language. Lists allow us to store and manipulate a collection of items, making them a valuable tool for many programming tasks. In this article, we will explore how to iterate over a list using indexes in Python.

First, let's start by understanding what indexes are in Python. An index is a number that represents the position of an item in a list. The first item in a list has an index of 0, the second item has an index of 1, and so on. This may seem counterintuitive at first, as we are used to counting from 1 in everyday life, but it is an important concept to understand when working with lists in Python.

Now, let's create a list in Python and see how we can iterate over it using indexes. We can create a list by enclosing a sequence of items in square brackets, like this:

```python

my_list = ['apple', 'banana', 'orange', 'grape', 'pineapple']

```

This list contains five items, and each item has a corresponding index from 0 to 4. To access an item in the list, we use its index in square brackets after the list's name. For example, to access the first item in the list, we would use `my_list[0]`, and to access the last item, we would use `my_list[4]`.

Now, let's say we want to print out each item in the list along with its index. We can do this by using a `for` loop and the `range()` function. The `range()` function takes in a start and end value and generates a sequence of numbers in between. In this case, we want the numbers to start at 0 and end at the length of our list, which we can get using the `len()` function. Our code would look like this:

```python

for i in range(len(my_list)):

print("Index:", i, "Item:", my_list[i])

```

This will print out each item in the list along with its corresponding index, like this:

```

Index: 0 Item: apple

Index: 1 Item: banana

Index: 2 Item: orange

Index: 3 Item: grape

Index: 4 Item: pineapple

```

Now, let's see how we can use indexes to manipulate our list. Let's say we want to change the third item in our list to 'strawberry'. We can do this by accessing its index, `2`, and reassigning its value, like this:

```python

my_list[2] = 'strawberry'

```

If we print out our list again, we can see that the third item has been changed to 'strawberry'.

```

['apple', 'banana', 'strawberry', 'grape', 'pineapple']

```

We can also use indexes to add or remove items from a list. To add an item, we can use the `insert()` method, which takes in the index where we want to insert the item and the item itself. For example, if we want to add 'kiwi' to our list at index 1, we would use the following code:

```python

my_list.insert(1, 'kiwi')

```

This will insert 'kiwi' at index 1 and shift all the other items to the right, like this:

```

['apple', 'kiwi', 'banana', 'strawberry', 'grape', 'pineapple']

```

To remove an item, we can use the `pop()` method, which takes in the index of the item we want to remove. If we want to remove the last item in our list, we can use the following code:

```python

my_list.pop(5)

```

This will remove 'pineapple' from our list, and the resulting list will be:

```

['apple', 'kiwi', 'banana', 'strawberry', 'grape']

```

In addition to using indexes in a `for` loop, we can also use them in a `while` loop to iterate over a list. We can set a variable `i` equal to 0 and use `while i < len(my_list)` as our condition. Inside the loop, we can perform any operations we want on the items in the list using their indexes. For example, if we want to print out all the items in the list in reverse order, we could do so using the following code:

```python

i = len(my_list) - 1

while i >= 0:

print(my_list[i])

i -= 1

```

Related Articles

Listing Even Numbers

<strong>Listing Even Numbers</strong> In the world of mathematics, numbers are classified into two categories: even and odd. Eve...

Creating a 2D Matrix in Python

Creating a 2D Matrix in Python Matrices are an essential data structure in mathematics and computer science. They are used to represent and ...