0
0
DynamodbConceptBeginner · 3 min read

What is List Type in DynamoDB: Explanation and Example

In DynamoDB, the List type is a flexible data type that stores an ordered collection of values, which can be of different types. It works like a shopping list where items keep their order and can be accessed by position.
⚙️

How It Works

The List type in DynamoDB is like a container that holds multiple items in a specific order. Imagine a grocery list where you write down items one after another; the order matters and you can have different types of items like numbers, strings, or even other lists inside it.

Each item in a List can be any DynamoDB data type, including nested lists or maps. This makes it very flexible for storing complex data structures. When you retrieve a list, you get the items in the same order you stored them, which is useful when order matters.

💻

Example

This example shows how to create a DynamoDB item with a List attribute containing different types of values.

python
import boto3

# Create DynamoDB client
client = boto3.client('dynamodb')

table_name = 'ExampleTable'

# Put item with a List attribute
response = client.put_item(
    TableName=table_name,
    Item={
        'ID': {'S': '123'},
        'MyList': {'L': [
            {'S': 'apple'},
            {'N': '10'},
            {'BOOL': True},
            {'L': [{'S': 'nested'}, {'N': '5'}]}
        ]}
    }
)

print('Item inserted with List attribute')
Output
Item inserted with List attribute
🎯

When to Use

Use the List type when you need to store multiple values in a specific order and the values can be of different types. For example, you might store a user's ordered preferences, a sequence of events, or a collection of mixed data like strings and numbers together.

It is especially useful when the order of items matters and you want to keep related data grouped inside a single attribute rather than separate attributes.

Key Points

  • Ordered collection: List keeps the order of items.
  • Flexible types: Items can be strings, numbers, booleans, maps, or even other lists.
  • Nested data: Lists can contain complex nested structures.
  • Use case: Best for ordered, mixed-type data stored together.

Key Takeaways

DynamoDB List type stores ordered collections of mixed data types.
Lists keep the order of items, useful for sequences or preferences.
List items can be simple or complex nested data structures.
Use List when you want to group related data in one attribute with order.