Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
How to reverse a String in Scala?
Next article icon

How to reverse a list in scala

Last Updated : 17 Apr, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

In Scala, list is defined under scala.collection.immutable package. A list is a collection of same type elements which contains immutable data. We use reverse function to reverse a list in Scala.
Below are the examples to reverse a list.

  • Reverse a simple list




    // Scala program to reverse a simple Immutable lists 
    import scala.collection.immutable._
      
    // Creating object 
    object GFG 
    { 
        // Main method 
        def main(args:Array[String]) 
        { 
            // Creating and initializing immutable lists 
            val mylist: List[String] = List("Geeks", "For",
                                    "geeks", "is", "best") 
          
            // Display the value of mylist1 
            println("Reversed List is: " + mylist.reverse) 
          
        } 
    } 
     
     

    Output:

    Reversed List is: List(best, is, geeks, For, Geeks)

     

  • Reverse a list using for loop




    // Scala program to print reverse immutable lists 
    // using for loop
    import scala.collection.immutable._
      
    // Creating object 
    object GFG
    { 
        // Main method 
        def main(args:Array[String]) 
        { 
            // Creating and initializing immutable lists 
            val mylist: List[String] = List("Geeks", "For", "geeks", "is",
                                            "a", "fabulous", "portal")
      
            // Display the value of mylist in
            // reverse order using for loop 
            for(element<-mylist.reverse) 
            { 
                println(element) 
            } 
        } 
    } 
     
     

    Output:

    portal  fabulous  a  is  geeks  For  Geeks

     

  • Reverse a list using foreach loop




    // Scala program to reverse a list
    // using foreach loop
    import scala.collection.immutable._
      
    // Creating object 
    object GFG
    { 
        // Main method 
        def main(args:Array[String]) 
        { 
            // Creating and initializing immutable lists 
            val mylist = List("Geeks", "For", "geeks", "is",
                            "a", "fabulous", "portal")
              
            print("Original list is: ")
              
            // Display the value of mylist using for loop 
            mylist.foreach{x:String => print(x + " ") }
              
            // calling reverse function
            println("\nReversed list: " + mylist.reverse)
        } 
    } 
     
     

    Output:

    Original list is: Geeks For geeks is a fabulous portal   Reversed list: List(portal, fabulous, a, is, geeks, For, Geeks)


Next Article
How to reverse a String in Scala?

S

Shivam_k
Improve
Article Tags :
  • Scala
  • Scala

Similar Reads

  • How to reverse a String in Scala?
    In this article, we will learn to reverse a string in Scala. Reverse of a string means changing its order so that the last character becomes the first, the second-to-last becomes the second, and so on, effectively flipping the string's sequence of characters. Examples: Input: S = "GeeksforGeeks"Outp
    3 min read
  • How to Sort a list in Scala?
    Sorting a list is a common operation in programming, and Scala provides convenient ways to accomplish this task. In this article, we'll explore different methods to sort a list in Scala, along with examples. Table of Content Using the sorted Method:Using the sortBy Method:Using the sortWith Method:S
    2 min read
  • How to Reverse keys and values in Scala Map
    In Scala, Map is same as dictionary which holds key:value pairs. In this article, we will learn how to reverse the keys and values in given Map in Scala. After reversing the pairs, keys will become values and values will become keys. We can reverse the keys and values of a map with a Scala for-compr
    2 min read
  • How to access list elements in Scala?
    In this articleIn this article, we will learn to access list elements in Scala. List in Scala is the most commonly used collection type, providing an ordered collection that allows accessing elements by index or using functional programming. How to Access List Elements in Scala?Below are the possibl
    3 min read
  • What are Folding Lists in Scala?
    A basic operation in functional programming, which includes Scala, is folding lists. It enables you to use a binary operation to merge the components of a collection. This operation applies the action to each member of the collection iteratively, building up a result from the original value. Table o
    4 min read
  • How to Merge Two Lists and Remove Duplicates in Scala?
    In Scala, lists are flexible data structures that are crucial for many programming tasks. They help in organizing and handling collections of items effectively. Knowing how to work with lists efficiently is vital for anyone coding in Scala. In this article, we'll explore how to combine two lists and
    3 min read
  • ListSet in Scala
    A set is a collection which only contains unique items which are not repeatable and a list is a collection which contains immutable data. In scala, ListSet class implements immutable sets using a list-based data structure. Elements are stored in reversed insertion order, That means the newest elemen
    6 min read
  • How to use java library in Scala?
    As Scala and Java are meant to work together effortlessly, using Java libraries in Scala programming is a breeze. To utilize a Java library in Scala, follow these steps: Importing Java Classes: Use Scala's import statement to import the required Java classes or packages.Creating Java Objects: Use Sc
    2 min read
  • Scala Queue reverse() method with example
    The reverse() method is utilized to return a queue with the reverse order. Method Definition: def reverse: Queue[A] Return Type: It returns a queue with the reverse order. Example #1: // Scala program of reverse() // method // Import Queue import scala.collection.mutable._ // Creating object object
    1 min read
  • How to get the first element of List in Scala
    In Scala, list is defined under scala.collection.immutable package. A list is a collection of same type elements which contains immutable data. Let's see how to get the first element of given List in Scala.List in Scala contains many suitable methods to perform simple operations like head(), tail(),
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences