Skip to content

RESOURCES / BLOG

How Do You Add to a Dictionary in Python?

Hi everyone,

I’m using Python to build a script that tracks image data, like filenames, tags, and URLs, after uploading them to Cloudinary. I want to store all this in a dictionary, but I’m not totally sure how to add new key-value pairs dynamically.

Can someone explain how to add to a dictionary in Python, maybe with examples involving image metadata or processing?

Thanks!

Great question! Dictionaries are one of Python’s most powerful and flexible data types, perfect for storing structured data like image names, upload URLs, and associated metadata.

Let’s walk through exactly how to add to a dictionary in Python with examples, especially those that are relevant for media and cloud workflows.

A dictionary is an unordered, mutable collection of key-value pairs. It’s like a mini-database stored in memory.

Example:

image_data = {

    "filename": "photo1.jpg",

    "tag": "portrait"

}Code language: JavaScript (javascript)

You can add a new key-value pair to a dictionary using simple assignment:

dictionary[key] = value

image_data = {}

image_data["filename"] = "photo1.jpg"

image_data["tag"] = "landscape"

print(image_data)

# Output: {'filename': 'photo1.jpg', 'tag': 'landscape'}Code language: PHP (php)

Imagine you just uploaded an image and want to store the filename, URL, and tag:

import cloudinary.uploader

upload_result = cloudinary.uploader.upload("photo1.jpg")

image_info = {}

image_info["filename"] = "photo1.jpg"

image_info["url"] = upload_result["secure_url"]

image_info["tag"] = "event"

print(image_info)Code language: JavaScript (javascript)

Now you have a complete dictionary storing that image’s metadata.

You can dynamically build a dictionary for multiple images:

image_files = ["img1.jpg", "img2.jpg"]

all_images = {}

for img in image_files:

    all_images[img] = {"tag": "gallery", "status": "pending"}

# Output: 

{

  'img1.jpg': {'tag': 'gallery', 'status': 'pending'},

  'img2.jpg': {'tag': 'gallery', 'status': 'pending'}

}Code language: PHP (php)

This is a common pattern when tracking bulk uploads or batch edits.

The update() method is another way to add multiple key-value pairs:

image_info = {"filename": "img1.jpg"}

image_info.update({"tag": "profile", "resolution": "1080p"})

# Output:  {'filename': 'img1.jpg', 'tag': 'profile', 'resolution': '1080p'}Code language: PHP (php)

It’s perfect for merging another set of metadata into an existing dictionary.

Let’s say you want to upload images and store each one’s data keyed by filename:

uploaded_data = {}

image_files = ["a.jpg", "b.jpg"]

for file in image_files:

    result = cloudinary.uploader.upload(file)

    uploaded_data[file] = {

        "url": result["secure_url"],

        "tag": "user_upload"

    }

print(uploaded_data)Code language: PHP (php)

This gives you a dictionary where each image is a key, and its metadata is a value; ideal for referencing later or exporting to CSV/JSON.

TaskMethodExample
Add single entrydict[key] = valued["tag"] = "nature"
Add multiple entriesdict.update({...})d.update({"tag": "urban", "size": "large"})
In loop with keysfor item in list: d[key] = valued[img] = {...}

Using a key that already exists will overwrite its value:

d = {"tag": "portrait"}

d["tag"] = "landscape"  # Now it's "landscape"Code language: PHP (php)

Dictionary keys must be immutable types (like strings, numbers, or tuples; not lists or other dictionaries).

Need to store more detailed info?

image_data = {

    "img1.jpg": {

        "tag": "event",

        "url": "https://res.cloudinary.com/your-img1"

    },

    "img2.jpg": {

        "tag": "profile",

        "url": "https://res.cloudinary.com/your-img2"

    }

}Code language: JavaScript (javascript)

You can easily add to nested structures like this:

image_data["img1.jpg"]["resolution"] = "4K"

Whether you’re storing image metadata, upload results, or analytics, using dictionaries in Python is a clean and efficient way to structure your data. Learning how to add to a dictionary in Python gives you full control over dynamic data, ideal for scripting around APIs like Cloudinary, local automation, or image categorization tools.

Start Using Cloudinary

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

Sign Up for Free