Reverse a List in Python
------------------------
# Option 1: Reverse a List in Python Using the reverse() Method
my_list = [1, 2, 3, 4, 5]
my_list.reverse() # In-place reversal
print(my_list) # Output: [5, 4, 3, 2, 1]
#We use the reverse() method, which modifies the original list directly and reverses the order of its elements.
#This method performs the reversal in-place without creating a new list, making it memory efficient.
#Option 2: Reverse a List Using Slicing in Python
my_list = [10, 20, 30, 40, 50]
reversed_list = my_list[::-1] # Reversing using slicing
print(reversed_list) # Output: [50, 40, 30, 20, 10]
#This creates a new list with the elements in reverse order, while keeping the original list unchanged.
#When we print reversed_list, the output will be [50, 40, 30, 20, 10]. This method is simple and does not affect the original list.
#Option 3: Reverse a List Using the reversed() Function in Python
my_list = [1, 2, 3, 4, 5]
reversed_list = list(reversed(my_list)) # reversed() returns an iterator
print(reversed_list)
#Option 4: Reverse a List in Python Using a For Loop
my_list = [15, 30, 45, 60]
reversed_list = []
index = len(my_list) - 1
while index >= 0:
reversed_list.append(my_list[index]) # Append elements from the end
index -= 1
print(reversed_list)
'''
Code Explanation:
Here is a step-by-step breakdown of the execution:
1. my_list = [1, 2, 3, 4, 5]: Initializes the starting list.
2. reversed_list = []: Creates an empty list to store the reversed elements.
3. for item in my_list:: Starts a loop that processes each number from left to right (1, then 2, then 3, etc.).
4. reversed_list.insert(0, item): This is the core logic.
• Step 1 (item = 1): Inserts 1 at index 0. → reversed_list becomes [1].
• Step 2 (item = 2): Inserts 2 at index 0, pushing 1 to the right. → reversed_list becomes [2, 1].
• Step 3 (item = 3): Inserts 3 at index 0, pushing the others down. → reversed_list becomes [3, 2, 1].
• This repeats until the loop finishes, resulting in [5, 4, 3, 2, 1].
'''
#Option 5: Reverse a List Using a While Loop in Python
my_list = [15, 30, 45, 60]
reversed_list = []
index = len(my_list) - 1
while index >= 0:
reversed_list.append(my_list[index]) # Append elements from the end
index -= 1
print(reversed_list)
'''
Code Explanation:
1. my_list = [15, 30, 45, 60]: Initializes the starting list.
2. reversed_list = []: Creates an empty list to store the reversed elements.
3. index = len(my_list) - 1: Sets the starting index to 3 (the last index of my_list, which points to 60).
4. while index >= 0:: Starts a loop that runs as long as the index is valid.
• Step 1 (index = 3): Appends my_list[3] (60) to reversed_list. Index becomes 2.
• Step 2 (index = 2): Appends my_list[2] (45) to reversed_list. Index becomes 1.
• Step 3 (index = 1): Appends my_list[1] (30) to reversed_list. Index becomes 0.
• Step 4 (index = 0): Appends my_list[0] (15) to reversed_list. Index becomes -1.
5. The loop terminates because -1 >= 0 is false. reversed_list outputs [60, 45, 30, 15].
'''
my_list = [1, 2, 3]
# Append treats the string as one item
my_list.append("cat")
print(my_list)
# Result: [1, 2, 3, 'cat']
# Extend unpacks the string character by character!
my_list.extend("dog")
# Result: [1, 2, 3, 'cat', 'd', 'o', 'g']
print(my_list)
To live a creative life we must lose the fear of being wrong.
Friday, October 16, 2026
Reverse a list in Python
Monday, October 5, 2026
How to remove and add .venv and requirements.txt in vs code
1. Open your project in VS Code
For example:
cd D:\vscode_python_practiceCheck your current environment:
python --versionand:
python -c "import sys; print(sys.executable)"2. Deactivate the current virtual environment
If you see:
(.venv) PS D:\vscode_python_practice>run:
deactivateThe prompt should become:
PS D:\vscode_python_practice>Verify:
python --version3. Create requirements.txt from the existing environment
Do this before deleting .venv if you want to preserve your currently installed packages.
If the old .venv is still available, activate it again temporarily:
.\.venv\Scripts\Activate.ps1Then:
pip freeze > requirements.txtExample requirements.txt:
numpy==2.x.x
pandas==2.x.x
matplotlib==3.x.x
pyspark==4.x.xThen deactivate:
deactivate4. Delete the old .venv
From your project directory:
Remove-Item -Recurse -Force .venvCheck that .venv is gone:
Get-ChildItemYou should still have:
D:\vscode_python_practice
│
├── requirements.txt
├── your_python_files.py
└── ...5. Install UV
First check whether UV is already installed:
uv --versionIf it says that uv is not recognized, install UV.
One common Windows installation method is:
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"Close and reopen the VS Code terminal afterward.
Verify:
uv --versionYou should get something similar to:
uv 0.x.x6. Check whether Python 3.15 is available
Run:
uv python listLook for:
cpython-3.15...You can also ask UV to install Python 3.15:
uv python install 3.15Then verify:
uv python list7. Create a UV environment using Python 3.15
From:
D:\vscode_python_practicerun:
uv venv --python 3.15This creates:
D:\vscode_python_practice\.venvusing Python 3.15.
You should see something similar to:
Using CPython 3.15...
Creating virtual environment at: .venv8. Activate the UV environment
On Windows PowerShell:
.\.venv\Scripts\Activate.ps1Your prompt should now show:
(.venv) PS D:\vscode_python_practice>9. Verify Python 3.15
Run:
python --versionExpected:
Python 3.15.xThen verify exactly which Python is being used:
python -c "import sys; print(sys.executable)"Expected:
D:\vscode_python_practice\.venv\Scripts\python.exe10. Install packages from requirements.txt
Now install your existing requirements:
uv pip install -r requirements.txtUV will install the packages into your active .venv.
You can verify:
uv pip listor:
pip list11. Point VS Code to the new UV environment
This is important.
Press:
Ctrl + Shift + PSearch:
Python: Select InterpreterSelect:
.venv\Scripts\python.exeIt should correspond to:
D:\vscode_python_practice\.venv\Scripts\python.exe12. Verify from VS Code
Open a new terminal.
You should see:
(.venv) PS D:\vscode_python_practice>Run:
python --versionThen:
python -c "import sys; print(sys.executable)"You should get:
Python 3.15.x
D:\vscode_python_practice\.venv\Scripts\python.exeComplete command sequence
If you already have requirements.txt, the clean process is:
cd D:\vscode_python_practice
deactivate
uv --version
uv python install 3.15
Remove-Item -Recurse -Force .venv
uv venv --python 3.15
.\.venv\Scripts\Activate.ps1
python --version
python -c "import sys; print(sys.executable)"
uv pip install -r requirements.txtThen in VS Code:
Ctrl + Shift + P
↓
Python: Select Interpreter
↓
.venv\Scripts\python.exeOne important recommendation for your PySpark setup
Before committing to Python 3.15, check your PySpark version:
findstr pyspark requirements.txtIf your project contains PySpark, Python 3.12 or 3.13 may currently be a much safer choice than 3.15 because the PySpark/Python compatibility matrix and third-party packages matter.
#################
(.venv) PS D:\vscode_python_practice> cd D:\vscode_python_practice
(.venv) PS D:\vscode_python_practice> python --version
Python 3.13.15
(.venv) PS D:\vscode_python_practice> python -c "import sys; print(sys.executable)"
D:\vscode_python_practice\.venv\Scripts\python.exe
(.venv) PS D:\vscode_python_practice> deactivate
PS D:\vscode_python_practice> python --version
Python 3.11.0
PS D:\vscode_python_practice> Remove-Item -Recurse -Force .venv
PS D:\vscode_python_practice> Get-ChildItem
Directory: D:\vscode_python_practice
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 29-09-2026 16:09 246 Program to show args and kwargs in python
copy.py
-a---- 29-09-2026 16:41 916 python Version Check.py
You should still have:
D:\vscode_python_practice
│
├── requirements.txt
├── your_python_files.py
└── ...
PS D:\vscode_python_practice> powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
downloading uv 0.12.23 (x86_64-pc-windows-msvc)
installing to C:\Users\rajanikanta\.local\bin
uv.exe
uvx.exe
uvw.exe
everything's installed!
To add C:\Users\rajanikanta\.local\bin to your PATH, either restart your shell or run:
set Path=C:\Users\rajanikanta\.local\bin;%Path% (cmd)
$env:Path = "C:\Users\rajanikanta\.local\bin;$env:Path" (powershell)
PS D:\vscode_python_practice>
Close VS Code and Reopen :
PS D:\vscode_python_practice> uv --version
uv 0.12.23 (46b84fd0b 2026-10-03 x86_64-pc-windows-msvc)
PS D:\vscode_python_practice> Test-Path .venv
False
PS D:\vscode_python_practice> uv venv .venv --python 3.13.15
Using CPython 3.13.15
Creating virtual environment at: .venv
Activate with: .venv\Scripts\activate
PS D:\vscode_python_practice> .\.venv\Scripts\Activate.ps1
(.venv) PS D:\vscode_python_practice> python --version
Python 3.13.15
(.venv) PS D:\vscode_python_practice> python -c "import sys; print(sys.executable)"
D:\vscode_python_practice\.venv\Scripts\python.exe
(.venv) PS D:\vscode_python_practice>
D:\vscode_python_practice
│
├── .venv
├── requirements.txt
└── your_python_file.py
(.venv) PS D:\vscode_python_practice> uv pip install -r requirements.txt
(.venv) PS D:\vscode_python_practice> uv pip install -r requirements.txt
Resolved 122 packages in 957ms
Prepared 122 packages in 12.97s
░░░░░░░░░░░░░░░░░░░░ [0/122] Installing wheels...warning: Failed to hardlink files; falling back to full copy. This may lead to degraded performance.
If the cache and target directories are on different filesystems, hardlinking may not be supported.
If this is intentional, set `export UV_LINK_MODE=copy` or use `--link-mode=copy` to suppress this warning.
Installed 122 packages in 52.96s
+ anyio==4.15.1
+ argon2-cffi==25.1.0
+ argon2-cffi-bindings==26.1.0
+ arrow==1.4.0
+ asttokens==3.0.2
+ async-lru==2.3.0
+ attrs==26.1.0
+ babel==2.18.0
+ beautifulsoup4==4.15.0
+ bleach==6.4.0
+ certifi==2026.7.22
+ cffi==2.1.1
+ charset-normalizer==3.5.2
+ colorama==0.4.6
+ comm==0.2.3
+ contourpy==1.4.0
+ cycler==0.12.1
+ debugpy==1.8.22
+ defusedxml==0.7.1
+ executing==2.2.1
+ fastjsonschema==2.22.2
+ filelock==4.0.11
+ fonttools==4.66.1
+ fqdn==1.6.0
+ fsspec==2026.9.0
+ h11==0.16.0
+ httpcore==1.0.9
+ httpx==0.28.1
+ huggingface-hub==0.36.2
+ idna==3.20
+ ipykernel==6.29.5
+ ipython==9.17.1
+ ipython-pygments-lexers==1.1.1
+ ipywidgets==8.1.9
+ isoduration==20.11.0
+ jedi==0.20.0
+ jinja2==3.1.6
+ json5==0.15.0
+ jsonpointer==3.1.1
+ jsonschema==4.25.0
+ jsonschema-specifications==2025.9.1
+ jupyter==1.1.1
+ jupyter-client==8.10.0
+ jupyter-console==6.6.3
+ jupyter-core==5.9.1
+ jupyter-events==0.12.1
+ jupyter-lsp==2.3.1
+ jupyter-server==2.21.1
+ jupyter-server-terminals==0.5.4
+ jupyterlab==4.4.10
+ jupyterlab-pygments==0.3.0
+ jupyterlab-server==2.28.1
+ jupyterlab-widgets==3.0.17
+ kiwisolver==1.5.1
+ lark==1.3.1
+ markdown-it-py==4.2.0
+ markupsafe==3.0.4
+ matplotlib==3.11.2
+ matplotlib-inline==0.2.2
+ mdurl==0.1.2
+ mistune==3.3.4
+ mpmath==1.3.0
+ nbclient==0.11.0
+ nbconvert==7.17.1
+ nbformat==5.11.1
+ nest-asyncio==1.6.0
+ networkx==3.7
+ notebook==7.4.4
+ notebook-shim==0.2.4
+ numpy==2.3.1
+ packaging==26.3
+ pandas==2.3.0
+ pandocfilters==1.5.1
+ parso==0.8.7
+ pillow==12.3.0
+ platformdirs==4.12.3
+ prometheus-client==0.26.0
+ prompt-toolkit==3.0.53
+ psutil==7.2.2
+ pure-eval==0.2.4
+ pycparser==3.0
+ pygments==2.21.0
+ pyparsing==3.3.3
+ python-dateutil==2.9.0.post0
+ python-dotenv==1.1.1
+ python-json-logger==4.2.0
+ pytz==2026.5
+ pywinpty==3.0.5
+ pyyaml==6.0.3
+ pyzmq==27.2.0
+ referencing==0.37.0
+ regex==2026.9.29
+ requests==2.32.4
+ rfc3339-validator==0.1.4
+ rfc3986-validator==0.1.1
+ rfc3987-syntax==1.1.0
+ rich==14.0.0
+ rpds-py==2026.9.1
+ safetensors==0.8.0
+ send2trash==2.1.0
+ setuptools==84.0.0
+ six==1.17.0
+ soupsieve==2.10
+ stack-data==0.6.3
+ sympy==1.14.0
+ terminado==0.18.1
+ tinycss2==1.5.1
+ tokenizers==0.21.4
+ torch==2.7.1
+ tornado==6.5.10
+ tqdm==4.70.1
+ traitlets==5.16.1
+ transformers==4.53.2
+ typing-extensions==4.16.0
+ tzdata==2026.5
+ uri-template==1.3.0
+ urllib3==2.8.0
+ wcwidth==0.9.2
+ webcolors==25.10.0
+ webencodings==0.6.1
+ websocket-client==1.9.2
+ widgetsnbextension==4.0.16
(.venv) PS D:\vscode_python_practice>
requirements.txt:
# Core environment
jupyter==1.1.1
notebook==7.4.4
ipykernel==6.29.5
# JSON validation
jsonschema==4.25.0
# Deep learning / GenAI
torch==2.7.1
transformers==4.53.2
# Data handling
numpy==2.3.1
pandas==2.3.0
matplotlib==3.11.2
# Utilities
rich==14.0.0
python-dotenv==1.1.1
requests==2.32.4