Updated April 13, 2023
Definition of Scala zipwithindex
zipwithindex in scala is used to bind the element with some counter. As the name suggests ‘zipwithindex’ is used when we want to have index for our list of the element in scala. In short, we can say zipwithindex is used to bind all the elements of the list present inside with some particular index. zipwithindex always returns a new collection in scala, also zipwithindex can be used with both types of list i.e. immutable and mutable collections. zipwithindex method always returns a tuple2 collection like an array in scala. In the coming section, we will see how this method can be used in programming.
Syntax:
zipwithindex method can be directly used on the immutable and immutable collection in scala and this method will give us a new tuple always with all the elements of the collection is bind with index.
Let’s see the syntax for zipwithindex method how we can use this in programming see below;
1) In this, we are calling foreach method with zipWithIndex, but we have mentioned the collection as well at the starting.
collection.zipWithIndex.foreach {
// our logic goes here for program.
}
2) In this syntax we are also creating a zipWithIndex by calling the zipWithIndex on the collection.
val myzicollection: Seq[(String, Int)] = collection_name.zipWithIndex
In the coming section, we will see a practice example of how we can use this in our programming to maintain the index in a collection data structure in Scala.
How zipwithindex Method works in Scala?
As now we know that zipwithindex method is used to deal with the indexing of the list element. This method helps us to create and generate the index of the mutable and immutable collection in Scala. With the help of these methods each and, every element present inside our collection got associated with an index. This is can also access the element by using their index. One more important thing about the zipwithindex method is that it always returns a new collection in Scala without being modified the existing collection on which we are calling it.
As we all know by the use of indexing searching and accessing of elements became easy and hence increase the performance of the program as well. Now we will discuss the complete signature of this method in detail how it looks and what does it return. Let’s see below for better understanding;
1) def zipWithIndex: Iterable[(A, Int)]: This is the signature of zipWithIndex method in Scala given by the Scala doc. This method is the member function of zipWithIndex class in Scala which has multiple super types which are as follows;
- Abstract view
- View
- Iterable
- IterableOnce
- IterableOps
- IterableOpsOnce
- AnyRef
- Any
Now let’s see how we can create a new tuple by using the zipWithIndex method on the collection see below:
e.g.:
val mylist = List("hello", "bye", "catch", "dog", "ele", "fan", "pink")
val zipWithIndexMyList = mylist.zipWithIndex
This is the basic example to use zipWithIndex in our program to create an index for our collection. Here we are creating a list that contains some elements inside it. We can assign any number of elements inside the list. After this, we are trying to bind these elements with the index but this is not an easy task to do if we do not have the zipWithIndex method available we need to write so many lines of code to achieve this functionality. But in Scala, we have the zipWithIndex method available so we can call this method directly on the collection, as you can see on the next line we are calling zipWithIndex with the collection variable name. So once we print out the output we will see it will create an index for every element present in the list. For example, the sample output will look like this see below;
e.g. :
(hello, 0),(bye, 1),(catch, 2),(dog, 3),(ele, 4),(fan, 5,(pink, 6)
In the above output as you can see it has created an extra variable with the element of the list. So in this manner, it works in Scala by the use of this we can directly access the list element without being them iterate. It will return us the new tuple without modified the existing collection in Scala.
points to remember while working with zipwithindex method in Scala;
- This method is used to create the index for the element.
- This method can be used with both types of collection data structure i.e. immutable and mutable.
- As the name suggests it will zip all the elements of the list with the newly created index with it, and returns a new collection for use.
Examples
1) In this example we are creating different types of lists in Scala and after this, we are calling zipWithIndex method to create the index for the list element, this will be a new list return by the zipWithIndex method in Scala. It contains list of String, integer, and so on for beginners.
Example #1
Code:
object Main extends App{
// Your code here!
println("demo to show zipwithindex method in Scala !!!")
//creating list of different types
val list1 = List("10", "20", "30", "40", "50", "60", "70")
val list2 = List("hello", "bye", "catch", "home", "world", "see you ")
val list3 = List(100, 200, 300, 400, 500, 00, 700, 800, 900)
//printing them for output
println("values inside the list 1 before zipwithindex method ::")
println(list1)
println("values inside the list 2 before zipwithindex method ::")
println(list2)
println("values inside the list 3 before zipwithindex method ::")
println(list3)
println("***********************************************************************************")
// calling zipWithIndex method to create new list and index for each element
val zip1 = list1.zipWithIndex
val zip2 = list2.zipWithIndex
val zip3 = list3.zipWithIndex
// printing the result after zipWithIndex method called.
println("values inside the list 1 after zipwithindex method ::")
println(zip1)
println("values inside the list 2 after zipwithindex method ::")
println(zip2)
println("values inside the list 3 aftre zipwithindex method ::")
println(zip3)
}
Output:
Conclusion
ZipwithIndex method is used to create the index in an already created collection, this collection can be mutable or immutable in Scala. After calling this method each and every element of the collection will be associate with the index value starting from the 0, 1,2, and so on. This will like an array type structure in Scala with value represented by the index with it.
Recommended Articles
We hope that this EDUCBA information on “Scala zipwithindex” was beneficial to you. You can view EDUCBA’s recommended articles for more information.