Friday, April 3, 2026

Java BufferedWriter Class With Examples

The Java BufferedWriter class is part of the java.io package and is widely used to write text efficiently to character‑output streams. Instead of writing characters directly to a file or output stream, BufferedWriter acts as a wrapper around a Writer (such as FileWriter or OutputStreamWriter) whose write() operations may be costly. BufferedWriter makes the write operation more efficient by buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.

Without buffering, every call to write() or print() would immediately convert characters into bytes and push them to the file system, which is inefficient. By using Java BufferedWriter, you reduce costly I/O operations and achieve faster, smoother file writing.

Java BufferedWriter constructors

  • BufferedWriter(Writer out)- Wraps the passed Writer and creates a buffering character-input stream that uses a default-sized input buffer. Default buffer size for BufferedWriter is 8192 bytes i.e. 8 KB.

    For example-

     BufferedWriter out = new BufferedWriter(new FileWriter("resources/abc.txt"));
     
  • BufferedWriter(Writer out, int sz)- Wraps the passed Writer and creates a new buffered character-output stream that uses an output buffer of the given size.

Java BufferedWriter methods

Methods in BufferedWriter class are as given below-

  • flush()- Flushes the stream.
  • newLine()- Writes a line separator.
  • write(int c)- Writes a single character.
  • write(char[] cbuf, int off, int len)- Writes a portion of an array of characters. Parameter off is the offset from which to start reading characters and parameter len is the number of characters to write.
  • write(String s, int off, int len)- Writes a portion of a String. Parameter off is the offset from which to start reading characters and parameter len is the number of characters to write.

Java BufferedWriter examples

For using BufferedWriter steps are as follows-

  1. Create an instance of BufferedWriter wrapped around another Writer.
  2. Using that instance to write to a file.
  3. Close the stream, you should close the resources in finally block or you can use try-with-resources to automatically manage resources.

1. Using write(int c) method of the Java BufferedWriter class to write single character to a file.

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriteRW {
  public static void main(String[] args) throws IOException {
    try (BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\Temp\\write.txt"))){
      bw.write(65); //writes A
      bw.write(66); //writes B
      bw.write(67); //writes C
    }
  }
}

2. Using write(char[] cbuf, int off, int len) method of the Java BufferedWriter class to write portion of a character array.

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriteRW {
  public static void main(String[] args) throws IOException {
    String str = "This is a test String";
    char[] buffer = str.toCharArray();
    try (BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\Temp\\write.txt"))){
      bw.write(buffer, 0, 7);// takes portion from index 0..6
    }
  }
}

3. Using write(String s, int off, int len) of the Java BufferedWriter class to write a portion of a String.

public class FileWriteRW {
  public static void main(String[] args) throws IOException {
    String str = "This is a test String";
    try (BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\Temp\\write.txt"))){
      bw.write(str, 0, 7);// takes substring from index 0..6
    }
  }
}

4. Using newLine() method of the Java BufferedWriter class.

public class FileWriteRW {
  public static void main(String[] args) throws IOException {
    String str = "This is a test String";
    try (BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\Temp\\write.txt"))){
      bw.write(str, 0, 7);// takes substring from index 0..6
      bw.newLine(); // new line
      bw.write(str, 7, str.length()-7);
      bw.flush(); // flushes the stream
    }
  }
}

That's all for this topic Java BufferedWriter Class With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Advanced Tutorial Page


Related Topics

  1. Character Streams in Java IO
  2. Byte Streams in Java IO
  3. Write to a File in Java
  4. How to Append to a File in Java
  5. Reading File in Java Using Files.lines And Files.newBufferedReader

You may also like-

  1. How to Read File From The Last Line in Java
  2. Difference Between Thread And Process in Java
  3. LinkedHashSet in Java With Examples
  4. String Vs StringBuffer Vs StringBuilder in Java
  5. Constructor Overloading in Java
  6. Python Program to Display Fibonacci Series
  7. Spring MVC Exception Handling Example Using @ExceptionHandler And @ControllerAdvice
  8. Lazy Initialization in Spring Using lazy-init And @Lazy Annotation

How to Sort ArrayList in Java

When you create an ArrayList in Java, elements are stored in the order they are inserted. By default, iterating over the list will display items in this insertion order. However, in real-world applications you often need to sort an ArrayList in Java, whether alphabetically, numerically, or by custom rules such as dates or objects.

In this tutorial, we’ll explore how to sort ArrayList in Java using different approaches available in Java. We’ll cover sorting Strings, Integers, and Dates in both ascending and descending order.


Options for sorting a List

Java provides multiple ways to sort a list:

  1. Using List.sort() (Java 8 and later)


    The sort() method was introduced as a default interface method in the List interface. It allows you to sort directly without using external utilities.

    list.sort(Comparator.naturalOrder());   // Ascending order
    list.sort(Comparator.reverseOrder());   // Descending order
    
    Method signature
    default void sort(Comparator<? super E> c)- If the comparator is null, elements must implement Comparable, and natural ordering will be applied.
  2. Using Collections.sort() method


    The traditional way to sort an ArrayList is via the Collections utility class. There are two overloaded versions of sort method and according to Java docs their description is-
    • public static <T extends Comparable<? super T>> void sort(List<T> list)- Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface. Furthermore, all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list).
    • public static <T> void sort(List<T> list, Comparator<? super T> c)- Sorts the specified list according to the order induced by the specified comparator. All elements in the list must be mutually comparable using the specified comparator (that is, c.compare(e1, e2)must not throw a ClassCastException for any elements e1 and e2 in the list).
    Usage-
    Collections.sort(list); // Natural ascending order
    Collections.sort(list, Comparator.reverseOrder()); // Custom order
    
  3. Using Java Stream API

    Using sorted method of the Java Stream API which provides a functional approach to sorting, especially useful when you want a sorted view without modifying the original list. There are two overloaded variants of the sorted method.
    • sorted()- Returns a stream consisting of the elements of this stream, sorted according to natural order.
    • sorted(Comparator<? super T> comparator)- Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator.

Sorting ArrayList of strings using sort() method of List

public class SortListDemo {
  public static void main(String[] args) {
    List<String> cityList = new ArrayList<>();
    cityList.add("Delhi");
    cityList.add("Mumbai");
    cityList.add("Bangalore");
    cityList.add("Chennai");
    cityList.add("Kolkata");
    cityList.add("Mumbai");
    // Passing null so natural ordering is used
    cityList.sort(null);
    System.out.println("List sorted using natural ordering" + cityList);
    
    // Using naturalOrder method to sort in natural order
    cityList.sort(Comparator.naturalOrder());
    System.out.println("List sorted using natural ordering" + cityList);
    
    // Using reverseOrder method to impose reverse of natural ordering
    cityList.sort(Comparator.reverseOrder());
    System.out.println("List sorted in reverse" + cityList);
  }
}

Output

List sorted using natural ordering[Bangalore, Chennai, Delhi, Kolkata, Mumbai, Mumbai]
List sorted using natural ordering[Bangalore, Chennai, Delhi, Kolkata, Mumbai, Mumbai]
List sorted in reverse[Mumbai, Mumbai, Kolkata, Delhi, Chennai, Bangalore]

Using Collections.sort() method to sort ArrayList

To sort an ArrayList of strings according to the natural ordering of its elements we can use the first of the two sort methods.

Collections.sort(List<T> list)
public class SortListDemo {
  public static void main(String[] args) {
    List<String> cityList = Arrays.asList("Delhi","Mumbai","Bangalore","Chennai","Kolkata","Mumbai");
    // sorting the list
    Collections.sort(cityList);
    System.out.println("List sorted using natural ordering" + cityList);
  }
}

Output

List sorted using natural ordering[Bangalore, Chennai, Delhi, Kolkata, Mumbai, Mumbai]

As you can see you just need to pass the ArrayList to sort method. Collections.sort will work in this case because String class implements Comparable interface and provides implementation for the method compareTo(String anotherString).

Same way Integer class or Date class also implements Comparable interface so list of integers (or dates) can also be sorted in natural order by using sort() method of the Collections class or by using sort() method of the List interface. In fact Java docs give a list of all the classes that implements comparable interface thus can be used with the sort method to sort the elements in the natural order.

Classes Implementing Comparable

Following is the list of classes that already implement Comparable interface in Java. Thus the ArrayList storing obejcts of any of these classes can be sorted in its natural ordering by passing the list to sort() method.

Class Natural Ordering
Byte Signed numerical
Character Unsigned numerical
Long Signed numerical
Integer Signed numerical
Short Signed numerical
Double Signed numerical
Float Signed numerical
BigInteger Signed numerical
BigDecimal Signed numerical
Boolean Boolean.FALSE < Boolean.TRUE
File System-dependent lexicographic on path name
String Lexicographic
Date Chronological
CollationKey Locale-specific lexicographic

Sorting an ArrayList of strings in descending order

Collections.sort() method always sorts ArrayList of strings in ascending order. For sorting an ArrayList in descending order you need to use the second sort method which takes two parameters. First is the list that has to be sorted and second a comparator class that can be used to allow precise control over the sort order.
For sorting an ArrayList in descending order there are two options,

  • Use method reverseOrder() provided by Collections class itself

    General form and description

    public static <T> Comparator<T> reverseOrder()
    
    Returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface.
  • Using a custom comparator.

Sorting ArrayList in descending order using reverseOrder method

public class SortListDemo {
  public static void main(String[] args) {
    List<String> cityList = Arrays.asList("Delhi","Mumbai","Bangalore","Chennai","Kolkata","Mumbai");
     // sorting the list in descending order
    Collections.sort(cityList, Collections.reverseOrder());
    System.out.println("List sorted in reverses order- " + cityList);
  }
}

Output

List sorted in reverses order- [Mumbai, Mumbai, Kolkata, Delhi, Chennai, Bangalore]

Sorting ArrayList in descending order using custom Comparator

Internally reverseOrder method calls a Comparator class to sort the list in reverse order. We can do it ourselves too by writing our own comparator class.

public class SortListDemo {
  public static void main(String[] args) {
    List<String> cityList = Arrays.asList("Delhi","Mumbai","Bangalore","Chennai","Kolkata","Mumbai");
     // sorting the list in descending order
    Collections.sort(cityList, (String a, String b)->  b.compareTo(a));
    System.out.println("List sorted in reverses order- " + cityList);
  }
}

Output

List sorted in reverses order- [Mumbai, Mumbai, Kolkata, Delhi, Chennai, Bangalore]

Note that Comparator is implemented as a lambda expression here.

Sorting Java ArrayList using sorted method of the Java Stream

public class SortListDemo {
  public static void main(String[] args) {
    List<String> cityList = Arrays.asList("Delhi","Mumbai","Bangalore","Chennai","Kolkata","Mumbai");
    List<String> tempList = cityList.stream().sorted().collect(Collectors.toList());
    System.out.println("List sorted in natural order- " + tempList);
    tempList = cityList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
    System.out.println("List sorted in reverse order- " + tempList);
  }
}

Output

List sorted in natural order- [Bangalore, Chennai, Delhi, Kolkata, Mumbai, Mumbai]
List sorted in reverse order- [Mumbai, Mumbai, Kolkata, Delhi, Chennai, Bangalore]

That's all for this topic How to Sort ArrayList in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. How ArrayList Works Internally in Java
  2. How to Join Lists in Java
  3. How to Remove Duplicate Elements From an ArrayList in Java
  4. Difference Between Comparable and Comparator in Java
  5. Java Collections Interview Questions And Answers

You may also like-

  1. Fail-Fast Vs Fail-Safe Iterator in Java
  2. How to Iterate a HashMap of ArrayLists of String in Java
  3. static import in Java
  4. Interface Static Methods in Java
  5. Method Reference in Java
  6. Count Number of Times Each Character Appears in a String Java Program
  7. Java ThreadLocal Class With Examples
  8. Try-With-Resources in Java With Examples

RunnableBranch in LangChain With Examples

RunnableBranch in LangChain conditionally branches to one of the Runnable chains based on the user input. RunnableBranch acts like an if-elif-else structure routing input to different chains based on a specified condition.

How RunnableBranch Works

You can pass a list of (condition, Runnable) pairs to the RunnableBranch class to create a conditional routing runnable that evaluates each condition in sequence and executes the corresponding chain, falling back to the default runnable if none of the conditions match. For example-

RunnableBranch(
    (lambda x: route_query(x["query"]) == "support", support_chain),
    (lambda x: route_query(x["query"]) == "sales", sales_chain),
    general_chain
)

LangChain RunnableBranch Example

This is the same example which was used in the RunnableLambda in LangChain With Examples post, covering the scenario where different specialized bots are used to handle different types of queries. A RunnableBranch ensures branching out to the support agent or sales agent based on the condition, with a "General" fallback for anything else.

Example Code

from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_ollama import ChatOllama
from langchain_core.runnables import RunnableBranch

support_template = "You are a support agent. User input: {query}. Provide 3 reasons for the issue."
sales_template = "You are a sales specialist. User input: {query}. Provide 3 reasons to buy the product and 3 for not buying."
general_template = "You are a general assistant. User input: {query}. Provide a helpful response."

model = ChatOllama(model="llama3.1", temperature=0.5)
# 1. Define specialized chains
support_chain = ChatPromptTemplate.from_template(support_template) | model | StrOutputParser()
sales_chain = ChatPromptTemplate.from_template(sales_template) | model | StrOutputParser()
general_chain = ChatPromptTemplate.from_template(general_template) | model | StrOutputParser()

# 2. Define routing logic 
def route_query(input: str) -> str:
    query = input.lower()
    print(f"Routing query: {query}")
    if "support" in query or "issue" in query:
        return "support"
    elif "price" in query or "buy" in query:
        return "sales"
    else:
        return "general"
    
branch = RunnableBranch(
    (lambda x: route_query(x["query"]) == "support", support_chain),
    (lambda x: route_query(x["query"]) == "sales", sales_chain),
    general_chain
)

# Example usage - Should go to support
response1 = branch.invoke({"query": "I have a login issue with my account."})
print(response1)

# Should go to sales
response2 = branch.invoke({"query": "Request to buy a 3D printer."})
print(response2)

# Should go to general chain
response3 = branch.invoke({"query": "Does company provide transportation services?"})
print(response3)

That's all for this topic RunnableBranch in LangChain With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. What is LangChain - An Introduction
  2. LangChain PromptTemplate + Streamlit - Code Generator Example
  3. LangChain PromptTemplate + Streamlit - Code Generator Example
  4. Messages in LangChain
  5. RunnableParallel in LangChain With Examples

You may also like-

  1. String in Java Tutorial
  2. Array in Java
  3. Count Number of Words in a String Java Program
  4. Ternary Operator in Java With Examples
  5. Java Multithreading Interview Questions And Answers
  6. Java Exception Handling Tutorial
  7. ConcurrentHashMap in Java With Examples
  8. TreeMap in Java With Examples

Thursday, April 2, 2026

Python Exception Handling - try,except,finally

Errors are inevitable in programming, but Python provides a powerful way to manage them gracefully through exception handling. In this article we’ll see how to handle exceptions in Python using try, except and finally statements to avoid abrupt crashes and ensures proper cleanup.

How Python Exception Handling Works

To handle exception in Python you can use the following procedure-

  1. try block: Wraps code that may raise an exception.
  2. except block: Catches and handles specific raised exceptions.
  3. finally block (optional): Runs regardless of whether an exception was raised or not, often used for cleanup tasks like closing files or releasing resources.

General Syntax of exception handling in Python using try, except finally is as follows.

try: 
 # code that may raise an exception
               
except ExceptionType1:
 # handle ExceptionType1
except ExceptionType2:
 # handle ExceptionType2
else:
 # executes if no exception occurs
finally: 
 # always executes (cleanup code)

Some important points to note here are-

  1. You can have multiple except blocks to handle multiple exceptions.
  2. Else and finally blocks are not mandatory.
  3. If no exception is raised then the statements in the else block are executed.
  4. try block must be followed either by except or finally.
  5. If a finally block is present, it always executes, whether or not an exception is raised.

Using try, except for exception handling

Let’s try to understand how exception handling in Python using try and except helps in writing robust code. Here is an example where a list is passed to a function and every element in the list is used to divide a number. Just notice what happens if one of the number in the list is zero.

def divide_num(num_list):
  for num in num_list:
    print(10/num)

num_list = [5, 6, 0, 7]
divide_num(num_list)

Output

2.0
1.6666666666666667
Traceback (most recent call last):

  File "F:/NETJS/NetJS_2017/Python/Test/Test.py", line 6, in <module>
    divide_num(num_list)
  File "F:/NETJS/NetJS_2017/Python/Test/Test.py", line 3, in divide_num
    print(10/num)
ZeroDivisionError: division by zero

As you can see dividing by zero raises ZeroDivisionError, since code doesn’t provide any exception handling code of its own so default exception handling is used. Default behavior for handling such exceptions in Python is for the interpreter to print the full stack traceback, that includes the exception type (at least in case of built-in exception) and error message, and then terminate the program.

As you can see program is terminated and only the exception message is displayed by the default exception handling which is not that user friendly.

Python exception handling with try, except

Now let’s write that same code by providing exception handling with in the code using try and except.

def divide_num(num_list):
  for num in num_list:
    try:
      print(10/num)
    except ZeroDivisionError as error:
      print(error)
      print('Zero is not a valid argument here')
    else:
      print('in else block')

num_list = [5, 6, 0, 7]
divide_num(num_list)

Output

2.0
in else block
1.6666666666666667
in else block
division by zero
Zero is not a valid argument here
1.4285714285714286
in else block

As you can see now the code that may throw an exception is enclosed with in a try block. When the exception is raised in the try block exception handler looks for the except block that can handle the exception of that type.

In the try block, ZeroDivisionError is raised when dividing by zero. Since there is an except block that handles ZeroDivisionError, that block executes and gracefully handles the exception.

Program is not terminated now when the exception is raised because of the exception handling in the code.

except block in Python

There are various ways to write except block while writing exception handling code in Python.

  1. You can catch the exception as an object that gives description about the raised exception.
     except ZeroDivisionError as error:
     
  2. You can write except block with the exception class name only.
     except Exception_Class_Name:
     
  3. You can handle multiple exceptions by using multiple except blocks or you can use a single except block and write multiple exceptions as a tuple.

    For example multiple except blocks-

    except ZeroDivisionError as error:
      print(error)
      print('Zero is not a valid argument here')
    except TypeError:
      print('Argument is not of valid type') 
      

    Single except block with multiple exceptions

      except (ZeroDivisionError, TypeError):
      
  4. You can write except with out specifying exception class. That way except block catches any type of exception. Though that is not considered good practice as it is too broad exception clause and you won’t be able to determine specifically which exception has been raised.
      except:
        print('An exception has occurred')
      

Multiple except block Python example

def divide_num(num_list):
  for num in num_list:
    try:
      print(10/num)
    except ZeroDivisionError as error:
      print(error)
      print('Zero is not a valid argument here')
    except TypeError as error:
      print(error)
      print('Argument is not of valid type-', num)

num_list = [5, 6, 'a', 7]
divide_num(num_list)

Output

2.0
1.6666666666666667
unsupported operand type(s) for /: 'int' and 'str'
Argument is not of valid type- a
1.4285714285714286

finally in Python exception handling

If a finally block is present, it acts as a "cleanup" handler. The finally clause is always executed whether an exception is raised in the try block or not. This guarantees that important cleanup tasks, such as closing files, releasing database connections, or freeing resources are performed consistently.

def divide_num(num):
  try:
    f = open("testfile", "a")
    r = 10/num
    f.write("Result 10/%d is %d" %(num,r))
  except ZeroDivisionError as error:
    print(error)
    print('Zero is not a valid argument here')
  except FileNotFoundError as error:
    print(error)
    print('Passed file does not exist')

  finally:
    print('closing file')
    f.close()

divide_num(0)

Output

division by zero
Zero is not a valid argument here
closing file

If function is called with a valid argument and no exception is raised even then finally block is executed. For example calling function as given here.

divide_num(2)

Output

closing file

If there is a return, break or continue statement in try block even then finally clause is executed.

def divide_num():
  try:
    return 'try'
  finally:
    print('In finally clause')

a = divide_num()
print(a)

Output

In finally clause
try

If there is a return statement in finally too then that becomes the last return statement to be executed. Since the return value of a function is determined by the last return statement executed so value returned by finally clause is the value returned by the function.

def divide_num():
  try:
    return 'try'
  finally:
    print('In finally clause')
    return 'finally'

a = divide_num()
print(a)

Output

In finally clause
finally

That's all for this topic Python Exception Handling - try,except,finally. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Python Tutorial Page


Related Topics

  1. raise Statement in Python Exception Handling
  2. User-defined Exceptions in Python
  3. Passing Object of The Class as Parameter in Python
  4. Abstract Class in Python
  5. Strings in Python With Method Examples
  6. Python Generator, Generator Expression, Yield Statement

You may also like-

  1. Magic Methods in Python With Examples
  2. Check if String Present in Another String in Python
  3. Keyword Arguments in Python
  4. Python Program to Display Fibonacci Series
  5. Java Collections Interview Questions And Answers
  6. this Keyword in Java With Examples
  7. What is Hadoop Distributed File System (HDFS)
  8. Spring Object XML Mapping (OXM) JAXB Example

Python for Loop With Examples

In Python, loops allow you to execute a block of code repeatedly. The two primary looping constructs are the for..in loop and the while loop. In this tutorial, we’ll focus on the Python for loop with coding examples, which is most commonly used to iterate over sequences such as strings, lists, tuples, sets, or dictionaries.


Syntax of Python for loop

for var in sequence:
 #for loop body
else:
 #else_suite
  • The for loop iterates through each element in the given sequence one by one.
  • On every iteration, the current element is assigned to the variable var, and the loop body executes.
  • Python also provides an optional else clause with the for loop. The else block runs only if the loop completes all iterations without encountering a break statement.

for loop flow

Python for Loop

Python for loop examples

1- If you want to display characters of a String using for loop in Python.

str = 'Test'
for ch in str:
    print(ch)

Output

T
e
s
t

2- Iterating Over a List

cities = ['New York', 'London', 'Mumbai', 'Paris', 'Madrid']
for city in cities:
    print(city)

Output

New York
London
Mumbai
Paris
Madrid

for loop with range sequence type

The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.

Syntax of range is-

range([start,] stop[, step])

If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0.

1- Iterating 0..9 using range and for loop.

for i in range(10):
    print(i)

Output

0
1
2
3
4
5
6
7
8
9

Since start and step arguments are omitted in the range so by default step is 1 and start is 0.

2- Display odd numbers between 1 and 10 using range and for loop in Python.

for i in range(1, 10, 2):
    print(i)

Output

1
3
5
7
9

Here start argument is 1 and step is 2.

3- To display all the elements in a list using index.

Using len function you can get the total number of elements in the list which becomes an argument for range.

cities = ['New York', 'London', 'Mumbai', 'Paris', 'Madrid']
for i in range(len(cities)):
    print(cities[i])

Output

New York
London
Mumbai
Paris
Madrid

Python for loop with else statement

In Python for loop has an optional else statement too. If the for loop runs till completion and terminates the else-suite is executed.

If for loop doesn’t run completely because of any of the following reason, else statement is not executed.

  • for loop is terminated abruptly due to a break statement
  • for loop is terminated abruptly due to a return statement
  • if an exception is raised

Here is an example showing a scenario where for loop with else statement can come handy. If you want to implement a functionality to find an element in array. If element is found in the array then its index is returned otherwise -1 is returned.

In this scenario you can use else statement with python for loop. If an element is found you will return its index so else suite won’t be executed. If for loop runs till completion that would mean searched element is not found in the array in that case else suite is executed and -1 is returned.

from array import *
def find_in_array(array, value):
    for index, e in enumerate(array):
        if e == value:
            return index
    # else associated with for loop
    else:
        return -1

a = array('i', [3,4,5,6,7])
find_value = 4
index = find_in_array(a, find_value)
if index != -1:
    print(find_value, 'found in array at index', index)
else:
    print(find_value, 'not found in array')

Output

4 found in array at index 1

Nested for loop

for loop can be nested which means you can have one for loop inside another. In the nested loops for each iteration of the outer for loop, inner for loop is iterated for the given range of elements.

Python Nested for loop example

In the example a pyramid pattern using * is displayed using nested for loops.

rows = 6
for i in range (1, rows):
    for j in range (0, rows -i):
        #print spaces, end='' ensures control 
        # remains on the same line
        print(' ', end='')
    for k in range (0, i):
        print('* ', end='')
    print()

Output

     * 
    * * 
   * * * 
  * * * * 
 * * * * * 

Though in Python same pyramid pattern can be displayed very elegantly using a single for loop.

rows = 6
for i in range (1, rows):
    #repeat space for given times 
    print(' '*rows, end='')
    print('* '*(i))
    rows = rows -1

That's all for this topic Python for Loop With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Python Tutorial Page


Related Topics

  1. Python Conditional Statement - if, elif, else Statements
  2. Python return Statement With Examples
  3. Python break Statement With Examples
  4. Method Overriding in Python
  5. Name Mangling in Python

You may also like-

  1. Passing Object of The Class as Parameter in Python
  2. Nonlocal Keyword in Python With Examples
  3. String Slicing in Python
  4. Python Exception Handling Tutorial
  5. Association, Aggregation And Composition in Java
  6. Why Class Name And File Name Should be Same in Java
  7. Just In Time Compiler (JIT) in Java
  8. BeanFactoryPostProcessor in Spring Framework

Decision Tree Regression With Example

In this post we'll see how to use decision tree regression which uses decision trees for regression tasks to predict continuous values. Decision trees are also used for creating classification models to predict a category or class label.

How does decision tree regression work

In decision tree regressor, decision tree splits the data using features and threshold values that enables them to capture complex, non-linear relationships.

The decision tree regression model has a binary tree like structure where you have a-

  1. Root node- Starting point which represents the whole dataset.
  2. Decision nodes- A decision point where the algorithm chooses a feature and a threshold to split the data into subsets.
  3. Branches- From each decision node there are branches to child nodes, representing the outcome of the rule (tested decision). For example, if you have housing dataset and one of the features is square footage and the threshold value is 1500 then the algorithm at a decision node decides- Is square footage ≤ 1500
    • If yes go towards left (houses smaller than 1500 sft)
    • If no go towards right (houses larger than or equal to 1500 sft).
  4. Leaf node- Contains the final predicted value. Also known as the terminal node.
Decision Tree Structure
Decision Tree Structure

How is feature selected

If there are multiple features, at each node only one of the features is selected for making the decision rule but that feature is not just picked arbitrarily by the algorithm. All of the features are evaluated, where the steps are as given below-

  1. For each feature, the algorithm considers possible split points (threshold values).
  2. For each candidate split, the algorithm computes the decrease in impurity after splitting. For decision tree regressor, impurity is measured using one of the following metrics-
    • Mean Squared Error (MSE) which is the default
    • Friedman MSE
    • Mean Absolute Error (MAE)
    • Poisson deviance

    When splitting a parent node into left (L) and right (R) child nodes:

    $$Cost of (\mathrm{split})=C(L)+C(R)$$

    The algorithm evaluates all possible features and thresholds, and chooses the split that minimizes the total squared error across child nodes

    For a parent node with N samples split into-

    • Left child with N_L samples
    • Right child with N_R samples

    The cost of the split is-

    $$C(\mathrm{split})=\frac{N_L}{N}\cdot MSE(L)\; +\; \frac{N_R}{N}\cdot MSE(R)$$

    where-

    $$MSE(L)=\frac{1}{N_L}\sum _{i\in L}(y_i-\bar {y}_L)^2$$ $$MSE(R)=\frac{1}{N_R}\sum _{i\in R}(y_i-\bar {y}_R)^2$$
    • yi = target value of sample i
    • \(\bar {y}_L, \bar {y}_R\) = mean target values in left and right child nodes
    • \(N=N_L+N_R\)
  3. The same procedure is repeated recursively for each child node until stopping criteria are met (e.g., max depth, min samples per leaf, max_leaf_node or no further improvement). Which means at each node:
    • Compute cost of split (C-split) for all candidate features and thresholds.
    • Choose the split with the minimum cost of split

    Scikit-Learn uses the Classification and Regression Tree (CART) algorithm to train decision trees

Here is the decision tree structure (with max depth as 3) for the laptop data used in the example in this post.

Decision Tree

How is the value predicted

Before going to how prediction is done in decision tree regressor, note that by following the decision rules at each node, samples will ultimately fall into one of the leaf nodes. Value you see in each of the leaf node in the above image is the average of the target values of all the training samples that ended up in that specific leaf node.

To make a prediction for a new data point, you traverse the tree from the root to a leaf node by following the decision rules. If you follow the above image at the root node it has chosen the "TypeName" feature (actually that TypeName feature is encoded that's why you see the feature name as "encoder_type_notebook"), threshold value is 0.5. Same way for all the other decision nodes there are some rules which will be evaluated for the new data point and ultimately the new data point also falls into one of the leaf nodes. So, the predicted value for the new data point will be the value in that leaf node (the average of the target values of all the training samples that ended up in that specific leaf node).

Decision tree regression using scikit-learn Python library

Dataset used here can be downloaded from- https://www.kaggle.com/datasets/illiyask/laptop-dataset

Goal is to predict the price of the laptop based on the given features.

In the implementation code is broken into several smaller units with some explanation in between for the steps.

1. Importing libraries and reading CSV file

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
df = pd.read_csv('./laptop_eda.csv')

laptop_eda.csv file is in the current directory.

2. Getting info about the data.

df.describe(include='all')

Analyze the data, there are 1300 rows, price has lots of variance, minimum value is 9270.72 whereas maximum value is 324954.72.

3. Check for duplicates and missing values

#for duplicates
df.duplicated().value_counts()
#for missing values
df.isnull().sum()

Output (for duplicates)

False    1270
True       30
Name: count, dtype: int64

There are duplicates which can be removed.

df.drop_duplicates(inplace=True)

4. Plotting pairwise relationship in the dataset

sns.pairplot(df[["Company", "Ram", "Weight", "SSD", "Price"]], kind="reg")
plt.show()

This helps in understanding the relationship between features as well as with dependent variables.

Decision Tree Regression

If you analyse the plots, relationship between Price and RAM looks kind of linear otherwise it is non-linear relationship among the pairs.

5. Checking for outliers

To check for extreme values IQR method is used. The IQR (Interquartile Range) method detects outliers by finding data points falling below Q1 - 1.5 X IQR or above Q3 + 1.5 X IQR

Here IQR = Q3 - Q1 (middle 50% of data)

  • Q1 is the 25th percentile
  • Q3 is the 75th percentile
for label, content in df.select_dtypes(include='number').items():
    q1 = content.quantile(0.25)
    q3 = content.quantile(0.75)
    iqr = q3 - q1
    outl = content[(content <= q1 - 1.5 * iqr) | (content >= q3 + 1.5 * iqr)]
    perc = len(outl) * 100.0 / df.shape[0]
    print("Column %s outliers = %.2f%%" % (label, perc))

Output

Column Ram outliers = 17.24%
Column Weight outliers = 3.54%
Column Touchscreen outliers = 100.00%
Column ClockSpeed outliers = 0.16%
Column HDD outliers = 0.00%
Column SSD outliers = 1.42%
Column PPI outliers = 9.37%
Column Price outliers = 2.20%

Going back to where data info was displayed and analysing it shows RAM values are from 2 GB to 64 GB, which to me looks ok in the context of laptop dataset and doesn't require dropping of any rows.

Touchscreen has only 2 values 0 and 1 (binary column), so that also doesn't require deletion of any rows as outliers.

Price also has lots of variance. You can also plot a distplot to look for distribution.

dp = sns.displot(df['Price'], kde=True, bins=30)
dp.set(xlim=(0, None))

Plot shows positive skewness but in this example, not deleting any outliers for Price. You can test the final result by keeping all the rows or after deleting outliers.

6. Feature and label selection

X = df.iloc[:, :-1]
y = df.iloc[:, -1]

7. Checking for multicollinearity

Multicollinearity check is generally not required for decision tree regression. Decision trees split the data based on thresholds of individual features. They don't estimate coefficients like linear regression does, so correlated predictors don't distort parameter estimates.

8. Splitting and encoding data

Splitting is done using train_test_split where test_size is passed as 0.2, meaning 20% of the data is used as test data whereas 80% of the data is used to train the model.

from sklearn.model_selection import train_test_split
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

ct = ColumnTransformer([
    ('encoder', OneHotEncoder(sparse_output = False, drop = 'first', handle_unknown = 'ignore'), X.select_dtypes(exclude='number').columns)
],remainder = 'passthrough')

X_train_enc = ct.fit_transform(X_train)
X_test_enc = ct.transform(X_test)

9. Training the model and predicting values

from sklearn.tree import DecisionTreeRegressor
regressor = DecisionTreeRegressor(max_depth=5, random_state=42)
regressor.fit(X_train_enc, y_train)

y_pred = regressor.predict(X_test_enc)
df_result = pd.DataFrame({'Actual':y_test, 'Predicted':y_pred})
df_result.head(10)

Output (side-by-side comparison of actual values and predicted values)

	 	Actual		Predicted
1176	114731.5536	90147.685886
1117	69929.4672	60902.414400
427		106506.7200	60902.414400
351		75071.5200	60902.414400
364		20725.9200	27029.100706
853		41931.3600	55405.469087
1018	118761.1200	55405.469087
762		60153.1200	83108.137838
461		39906.7200	55405.469087
883		19660.3200	20666.681974

10. Seeing the model metrics such as R squared to check whether the model is overfitting or not.

from sklearn.metrics import r2_score, mean_squared_error
# for training data
print(regressor.score(X_train_enc, y_train))
#for predicted values
print(r2_score(y_test, y_pred))

Output

0.8003266191317047
0.7163190370541831

R2 score for training is 0.8 where as for test data it is 0.71

If the training score were very high (close to 1.0) and the test score much lower (like 0.3–0.4), that would indicate overfitting.

If both scores were low (say <0.5), that would indicate the model is too simple and not capturing the patterns.

The gap between 0.80 and 0.71 is modest. This means a slight overfitting, but not in extreme. The model generalizes reasonably well.

11. Plotting the tree

If you want to check how decision nodes were created by the algorithm you can plot the decision tree.

from sklearn.tree import plot_tree
plt.figure(figsize=(20,15)) 
plot_tree(regressor, filled=True, fontsize=10)
plt.show()

Another way to do it is by using the graphviz library. But that would mean downloading graphviz from this location- https://graphviz.org/download/

Also requires setting the path to the bin directory which can be done programmatically.

feature_names = ct.get_feature_names_out()
# get the name of the features used (after encoding)
X_train_final = pd.DataFrame(X_train_enc, columns=feature_names, index=X_train.index)

from sklearn.tree import export_graphviz
dot_data = export_graphviz(
regressor,
out_file=None,
feature_names=X_train_final.columns,
rounded=True,filled=True
)
import os
#setting path
os.environ["PATH"] += os.pathsep + r"D:\Softwares\Graphviz-14.1.1-win64\bin"
from graphviz import Source
Source(dot_data)

That's all for this topic Decision Tree Regression With Example. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Python Tutorial Page


Related Topics

  1. Simple Linear Regression With Example
  2. Multiple Linear Regression With Example
  3. Polynomial Regression With Example
  4. Support Vector Regression With Example
  5. Mean, Median and Mode With Python Examples

You may also like-

  1. Passing Object of The Class as Parameter in Python
  2. Local, Nonlocal And Global Variables in Python
  3. Python count() method - Counting Substrings
  4. Python Functions : Returning Multiple Values
  5. Marker Interface in Java
  6. Functional Interfaces in Java
  7. Difference Between Checked And Unchecked Exceptions in Java
  8. Race Condition in Java Multi-Threading

RunnableLambda in LangChain With Examples

RunnableLambda in LangChain is one of the core components of the LangChain Expression Language (LCEL). What RunnableLambda offers is that it can convert a Python (or JavaScript) function or a lambda expression into a Runnable object. This allows custom functions to be seamlessly integrated into the chain ecosystem of LangChain alongside other LangChain components like models, prompt templates, and retrievers.

LangChain RunnableLambda example

One of the prominent usecase depicting the usage of RunnableLambda is the implementation of routing in LangChain.

In LangChain there is a dedicated class called LLMRouterChain, which lets you use an LLM-powered chain to decide how inputs should be routed to different destinations. But this class is deprecated in latest versions. RunnableLambda is one of the way now for implementing routing as it provides several other benefits like streaming and batch support.

1. In the first example, we’ll have two chains, one which handles geography related queries and another which handles history related queries. Using RunnableLambda we can route to one of these chains based on whether the question is history based or geography based. That classification of the query is also done using the LLM. Note that Groq inference provider is used here which requires langchain-groq package installation and setting up the “GROQ_API_KEY” value.

from langchain_core.runnables import RunnableLambda
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_groq import ChatGroq
from typing_extensions import TypedDict
from operator import itemgetter
from typing import Literal

history_prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a history expert."),
        ("human", "{query}"),
    ]
)
geography_prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a geography expert."),
        ("human", "{query}"),
    ]
)

model = ChatGroq(model="qwen/qwen3-32b", temperature=0.5)

chain_history = history_prompt | model | StrOutputParser()
chain_geography = geography_prompt | model | StrOutputParser()

route_system = "Classify the user's query to either the history "
"or geography related. Answer with one word only: 'history' or 'geography'."
route_prompt = ChatPromptTemplate.from_messages(
    [
        ("system", route_system),
        ("human", "{query}"),
    ]
)

#Class to enforce structured output from LLMs
class RouteQuery(TypedDict):
    """Schema for LLM output for routing queries."""
    destination: Literal["history", "geography"]

route_chain = (
    route_prompt
    | model.with_structured_output(RouteQuery)
    | itemgetter("destination")
)

final_chain = {
    "destination": route_chain,  
    "query": lambda x: x["query"],  # pass through input query
} | RunnableLambda(
    # if history, chain_history. otherwise, chain_geography.
    lambda x: (
        # display the routing decision for clarity
        print(f"Routing to destination: {x['destination']}") or
        (chain_history if x["destination"] == "history" else chain_geography)
    )
)

result = final_chain.invoke({"query": "List 5 temples built by the Cholas. Only list the temples, no other information. "})

print(result)

Output

Routing to destination: history
1. Brihadeeswara Temple, Thanjavur
2. Brihadeeswara Temple, Gangaikonda Cholapuram
3. Airavatesvara Temple, Darasuram
4. Nellaiappar Temple, Tirunelveli
5. Siva Temple, Kudavai

Points to note here:

  • The Literal type in Python from typing module, is a type hint that indicates a variable or function parameter must have one of a specific set of fixed, concrete values. Since the output of the LLM should be one of 'history' or 'geography' so that is enforced by passing these fixed values.
  • TypedDict is also used to enforce a structured output format.
  • The first part of the final chain
    {
        "destination": route_chain,  
        "query": lambda x: x["query"],  # pass through input query
    }
    
    is a dictionary that defines a mapping of inputs to outputs for the chain. The key "destination" will be populated by running route_chain.
  • In the second part of the chain, RunnableLambda wraps a lambda expression that selects the appropriate chain (chain_history or chain_geography) based on the "destination" value.

2. Second example also shows how to use RunnableLambda for routing. In the first example classification of the query is done by the LLM but in this one it is done by a Python function, which is wrapped into a RunnableLambda. Routing is done to one of the 3 agents based on the output of the Python function.

from langchain_core.runnables import RunnableLambda
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_ollama import ChatOllama

support_template = "You are a support agent. User input: {query}. Provide 3 reasons for the issue."
sales_template = "You are a sales specialist. User input: {query}. Provide 3 reasons to buy the product and 3 for not buying."
general_template = "You are a general assistant. User input: {query}. Provide a helpful response."
# 1. Define specialized chains
support_chain = ChatPromptTemplate.from_template(support_template) | ChatOllama(model="llama3.1") | StrOutputParser()
sales_chain = ChatPromptTemplate.from_template(sales_template) | ChatOllama(model="llama3.1") | StrOutputParser()
general_chain = ChatPromptTemplate.from_template(general_template) | ChatOllama(model="llama3.1") | StrOutputParser()

# 2. Define routing logic using RunnableLambda
def route_query(input: dict):
    print(f"Routing input: {input}")
    query = input["query"].lower()
    print(f"Routing query: {query}")
    if "support" in query or "issue" in query:
        return support_chain
    elif "price" in query or "buy" in query:
        return sales_chain
    else:
        return general_chain

router_node = RunnableLambda(route_query)

print("Router node created.", type(router_node))

# 3. Create the final routing chain
# The router_node returns a chain, which is then invoked with the input
final_chain = router_node 

# Example usage
result = final_chain.invoke({"query": "Request to buy a 3D printer."})

print(result)

Here’s what happens internally:

  • router_node runs route_query(input).
  • That function returns the right chain (support_chain, sales_chain, or general_chain).
  • Because router_node is a runnable, it automatically invokes the returned chain with the same input.

That's all for this topic RunnableLambda in LangChain With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. RunnableParallel in LangChain With Examples
  2. LangChain PromptTemplate + Streamlit - Code Generator Example
  3. Messages in LangChain
  4. Chain Using LangChain Expression Language With Examples

You may also like-

  1. String in Java Tutorial
  2. Array in Java
  3. Count Number of Words in a String Java Program
  4. Ternary Operator in Java With Examples
  5. Java Multithreading Interview Questions And Answers
  6. Java Exception Handling Tutorial
  7. ConcurrentHashMap in Java With Examples
  8. TreeMap in Java With Examples