Fundamentals 2 min read

How to Reverse a Number into a List Using Recursion in Python

This article explains how to transform a number like 1234 into a reversed list of its digits using both a straightforward string method and two recursive implementations—standard and tail recursion—while noting Python's general preference for iterative solutions.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Reverse a Number into a List Using Recursion in Python

Question: Convert a number into a reversed list of its digits, e.g., 1234 → [4,3,2,1], using recursion.

Simple method: cast the integer to a string, reverse it, and convert each character back to an integer.

Standard recursion

def reverse_order_list1(lst:list, tmp=[]):
    if len(lst) == 0:
        return tmp
    num = lst.pop()
    tmp.append(int(num))
    return reverse_order_list1(lst, tmp=tmp)
print(reverse_order_list1(list(str(1234))))

Tail recursion

def reverse_order_list2(lst:list, tmp=[]):
    if len(lst) > 0:
        num = lst.pop()
        tmp.append(int(num))
        reverse_order_list2(lst, tmp=tmp)
    return tmp
print(reverse_order_list2(list(str(1234))))

Note: In Python, recursion is generally discouraged for such tasks because loops are more efficient, but recursion can be convenient in certain scenarios.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

algorithmRecursionListreverse
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.