Hey Python folks,
I’m working on a script that handles a bunch of images (filenames, tags, URLs), and I want to build a list of them as I process or upload each one. I keep seeing people use “append”’ with lists, but I’m not totally sure how it works.
Can someone explain how append works in Python (and when to use it)? Especially helpful if you can give examples with image processing or something like Cloudinary.
Thanks!
Absolutely, and great question! If you’re working with lists in Python, whether you’re storing image URLs, tags, or filenames then understanding append() is essential.
Let’s break it down step-by-step.
In Python, append() is a list method used to add an element to the end of a list. It modifies the original list in-place (no need to reassign).
your_list.append(item)
images = []
images.append("image1.jpg")
images.append("image2.jpg")
print(images)
# Output: ['image1.jpg', 'image2.jpg']Code language: PHP (php)
This is perfect when you’re collecting image filenames or metadata during processing.
Let’s say you’re uploading images and want to keep track of the URLs:
import cloudinary.uploader
uploaded_urls = []
image_files = ['photo1.jpg', 'photo2.jpg']
for file in image_files:
response = cloudinary.uploader.upload(file)
uploaded_urls.append(response['secure_url'])
print(uploaded_urls)Code language: JavaScript (javascript)
After running this, uploaded_urls will contain a list of image URLs returned by Cloudinary.
You can append any data type to a list: strings, numbers, dictionaries, or even other lists.
tags = []
tags.append("portrait")
tags.append("landscape")Code language: JavaScript (javascript)
image_data = []
image_data.append({"filename": "img1.jpg", "tag": "sunset"})
image_data.append({"filename": "img2.jpg", "tag": "urban"})
print(image_data[0]['tag']) # Output: sunsetCode language: PHP (php)
Appending in a loop is one of the most common use cases:
filenames = ['img1.jpg', 'img2.jpg', 'img3.jpg']
image_tags = []
for name in filenames:
tag = name.split('.')[0] # Just a sample tag logic
image_tags.append(tag)
print(image_tags)
# Output: ['img1', 'img2', 'img3']Code language: PHP (php)
append()adds one item to the end of the list.extend()adds each element of another iterable individually.
a = [1, 2]
a.append([3, 4]) # [1, 2, [3, 4]]
a = [1, 2]
a.extend([3, 4]) # [1, 2, 3, 4]Code language: PHP (php)
You don’t need to do this:my_list = my_list.append("something") # Wrong: returns None
Just:
my_list.append("something")
- Building lists dynamically during processing.
- Collecting filenames, URLs, or tags.
- Storing user inputs or results.
- Gathering data in a for-loop.
Here’s a realistic use case if you’re doing batch image processing:
processed_images = []
def process_image(file):
# Dummy logic
return {"filename": file, "status": "done"}
image_files = ['img1.jpg', 'img2.jpg']
for img in image_files:
result = process_image(img)
processed_images.append(result)
print(processed_images)
# Output:
[
{'filename': 'img1.jpg', 'status': 'done'},
{'filename': 'img2.jpg', 'status': 'done'}
]Code language: PHP (php)
| Operation | Syntax | Result |
| Add item to list | my_list.append(item) | Adds item at the end |
| Add multiple items | Loop with append() | Collects one item at a time |
| Add dicts to a list | list.append({...}) | Store structured image metadata |
| Append vs. Extend | append([x]) vs extend([x]) | One item vs multiple from a list |
The append() method in Python is one of the most used and most essential tools when working with lists. Whether you’re collecting Cloudinary URLs, storing filenames, tagging images, or logging user actions, append() gives you an easy way to grow your list as your data grows.