Ask Sawal

Discussion Forum
Notification Icon1
Write Answer Icon
Add Question Icon

How to access elements of tuple in python?

1 Answer(s) Available
Answer # 1 #

The zip function returns a tuple iterator, which is used to find the values at the same position of two or more iterable items.

The name of the function is zip, which is a visual word. Think of the two parts of a zipper that have their teeth interlocked as it closes.

We see it with an example when we return to our field. Imagine we have a list with the values and another with the values.

We will get an iterator with the values when we pass these two lists as arguments.

The first position of each of the lists has a tuple that combines the values of the first position.

The second position of the lists has another tuple with the values. You know, in the third.

We can use the iterator to access the values of both lists.

I present two different versions, one in which we directly access the iterator and the other in which we directly access the values.

If we execute the previous code, either of the two loops will generate the same result on the screen.

It is very comfortable.

If you need to save the list of tuples, you can call the list function in this way. If the combined list is displayed on the screen, we will get the following:

The enumerate function can be used if we need more control over the iteration.

The enumerate function returns a tuple iterator with a counter and a piece of data from the collection we are traversing. In the same loop, the list is used to get the index and its data.

The truth is that this function is very convenient and doesn't have to be defined or controlled. We show an example so that you understand it better.

We get the following result when we run the above code

In this example, you will say that there is only one list and that it is about traversing two lists at the same time. There's a trick. We have an index variable that tells us the position of the current element in the loop. We can use that index to find the value of the other list.

And we will get as a result.

This option gives us more flexibility because we can use the index for whatever we need to. We need the index since the values we get through the zip or enumerate are copies of the values of the lists.

If we want to walk the walk in a more classical way, we can do that. I have two alternatives for you.

The first option is to use a for loop that loops through the numbers between 0 and the length of the lists. The len function gives us the length of the lists and the range function, so we will use this to do this.

In each loop of the loop, we have each of the indexes of the lists between 0 and their length. We can use that index to access the data.

The most flexible option is the one that uses a while loop and controls the index variable to make sure it takes the position values of the lists.

We do it in a certain way.

The two forms are more flexible than the previous ones because the index allows us to make the traversals not be parallel, that is, element by element and in the same order, if we include the option with enumerate.

For example, we might want to go back and forth between a list element by element and another two elements by two, or we might want to go back and forth between a list element by element and another two elements by two.

This type of tours are more comfortable if we have the value of the indices.

There are many times when we need to go through multiple lists at the same time.

The truth is that it gets more complicated because I have told you that everything is applicable to multiple lists.

The zip function can take more than two lists as parameters and return a tuple with the values of one position from all the lists. The example is:

If we need the values of the indices in addition to the route, we can access multiple lists within the body of the loop, the same way we can access multiple lists if we want to modify the value of the lists.

Combining the use of enumerate and zip is a possibility.

[1]
Edit
Query
Report
Sharafat Shaman
SUPERVISOR BOILERMAKING