What is List Type in DynamoDB: Explanation and Example
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.
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')
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.