Skip to content

RESOURCES / BLOG

How to Make a List in Python

Hey everyone,

I’m just starting to learn Python and working on a script that processes multiple image files. I was told that I need to store file names or tags in a list, but I’m a bit confused by the syntax.

Can someone explain how to make a list in Python, with beginner-friendly examples? Maybe something that applies to image handling or filenames?

Thanks!

Lists are one of the most fundamental and useful data types in Python. Whether you’re managing image filenames, objects, or even simple strings, lists will become your go-to structure for storing and organizing data.

Let’s walk through everything you need to know about how to make a list in Python, with examples tailored to image and media workflows.

A list is a collection of items that are ordered and changeable (mutable). Lists allow duplicate items and can contain elements of different types (strings, numbers, objects, even other lists).

Think of a list like a storage container, great for holding file names, metadata, tags, or upload responses into a single variable. They allow you to access individual items within the list, a range of elements, or the whole thing.

Lists are created by adding any number of elements between square brackets, like so:

my_list = [item1, item2, item3]

You can also use the list() function, which takes an iterable (like a dictionary, tuple, or string) and returns it as a list object instead.

some_tuple = (1, 2, 3)

new_list = list(some_tuple)

print(new_list)

# output: [1, 2, 3]Code language: PHP (php)
images = ["sunrise.jpg", "beach.png", "clouds.webp"]

print(images)

# output: ['sunrise.jpg', 'beach.png', 'clouds.webp']Code language: PHP (php)

Lists of strings are useful for image labels, categories, or Cloudinary tags:

tags = ["nature", "sunset", "travel"]

For cases like image IDs or width/height dimensions:

dimensions = [1920, 1080]

You can mix data types too, though it’s not always recommended for clarity:

mixed = ["image.jpg", 2048, True]

You can also create an empty list and add items later:

uploads = []

uploads.append("image1.jpg")

uploads.append("image2.jpg")

print(uploads)

# output: ['image1.jpg', 'image2.jpg']Code language: PHP (php)

This is especially useful when looping over files or API responses.

Let’s say you’re working with a set of 5 numbered images:

images = []

for i in range(1, 6):

    images.append(f"photo_{i}.jpg")

print(images)

# output: ['photo_1.jpg', 'photo_2.jpg', 'photo_3.jpg', 'photo_4.jpg', 'photo_5.jpg']Code language: PHP (php)

Or use a list comprehension (more Pythonic!):

images = [f"photo_{i}.jpg" for i in range(1, 6)]

Imagine you’re uploading a batch of images to Cloudinary:

image_files = ["hero.jpg", "banner.png", "logo.svg"]

for file in image_files:

    # Hypothetical upload function

    upload_to_cloudinary(file)Code language: PHP (php)

Using a list allows you to easily loop over your items.

paths = ["images/pic1.jpg", "images/pic2.jpg", "images/pic3.jpg"]

tags = []

tags.append("portrait")

tags.append("high-res")Code language: JavaScript (javascript)

tags.remove("portrait")

images = ["b.jpg", "a.jpg", "c.jpg"]

images.sort()Code language: JavaScript (javascript)
uploaded_ids = []

image_files = ["1.jpg", "2.jpg", "3.jpg"]

for file in image_files:

    result = cloudinary.uploader.upload(file)

    uploaded_ids.append(result["public_id"])

print("Uploaded IDs:", uploaded_ids)Code language: PHP (php)

This shows how powerful and practical lists can be when working with Python (or any programming language).

MistakeFixWhy?
Using parentheses: ()Use square brackets: []This creates a tuple, a different type of data structure.
Forgetting commas between itemsAdd commas: ["img1", "img2"]This will throw a syntax error in most cases, as elements need to be separated by commas.
Modifying lists while iteratingUse .copy() or store results separatelyCan cause unexpected behaviors or throw errors.
TaskCode Example
Make list of itemsmy_list = ["one", "two", "three"]
Add itemmy_list.append("four")
Loop through itemsfor item in my_list: print(item)
Create from stringlist("abc") → ['a', 'b', 'c']
List from rangelist(range(5)) → [0, 1, 2, 3, 4]
List comprehension[f"img{i}.jpg" for i in range(3)]

Whether you’re processing a batch of images, building a gallery view, or uploading assets to Cloudinary, lists in Python make your workflow clean, flexible, and efficient. Once you know how to make a list in Python, you unlock the foundation for working with sets of data, be it images, tags, or file paths.

Start Using Cloudinary

Sign up for our free plan and start creating stunning visual experiences in minutes.

Sign Up for Free