R
Rishtaara
Python: Zero to Professional
Lesson 25 of 40Article12 min

Python How Tos — Duplicates, Reverse & Add

Convert to set and back: list(dict.fromkeys(items)) preserves order in Python 3.7+.

Remove List Duplicates

Convert to set and back: list(dict.fromkeys(items)) preserves order in Python 3.7+.

Or loop and append only if not seen — full control over which duplicate to keep.

Real-life example: Removing duplicates is deduplicating a guest list — each person invited once.

Unique ordered list
items = [1, 2, 2, 3, 1, 4]
unique = list(dict.fromkeys(items))
print(unique)  # [1, 2, 3, 4]

Reverse a String

Slicing [::-1] reverses any sequence — strings, lists, tuples.

Or use reversed() and join for strings: ''.join(reversed(text)).

Real-life example: Reversing a string is reading a sign in a mirror — last letter becomes first.

String reverse
text = "Python"
print(text[::-1])
print("".join(reversed(text)))

Add Two Numbers

Simple: a + b. For user input, cast with int() inside try/except.

This is a classic first logic exercise — foundation for calculators.

Real-life example: Adding two numbers is a cashier totaling two items — basic but everywhere.

Add with input
a = int(input("First number: "))
b = int(input("Second number: "))
print("Sum:", a + b)