List
Creating a List
s = [10, "math", 2022.01.01T09:10:01, [a, 22, 3.5]]
type(s)
Output: list
Accessing List Items
-
Access a list item by referring to the position.
s[3]
Output: [a, 22, 3.5]
s[-2]
Output: 2022.01.01T09:10:01
-
Access list items by slicing. Note that the step parameter cannot be specified in Python Parser.
s[1:4]
Output: ['math', 2022.01.01T09:10:01, [a, 22, 3.5]]
s[2:]
Output: [2022.01.01T09:10:01, [a, 22, 3.5]]
Updating a List
-
Changing list items
s[1]="english"
Output: [10, 'english', 2022.01.01T09:10:01, [a, 22, 3.5]]
// updating a specific item in a nested list through multiple indices is not supported. s[3][1] = 21 // unsupported // Rewrite as: tmp = s[3] tmp[1] = 21 s
Output: [10, 'english', 2022.01.01T09:10:01, [a, 21, 3.5]]
-
Adding list items
s.append(2020.01.01)
Output: [10, 'english', 2022.01.01T09:10:01, [a, 21, 3.5], 2020.01.01]
s.insert(0, 13:30m)
Output: [13:30m, 10, 'english', 2022.01.01T09:10:01, [a, 21, 3.5], 2020.01.01]
Removing List Items
The del
keyword is not supported in Python Parser.
s.pop() // Remove the specified index. If the index is not specified, remove the last item.
Output: 2020.01.01
s
Output: [13:30m, 10, 'english', 2022.01.01T09:10:01, [a, 21, 3.5]]
s.remove(13:30m) // Remove the specified item.
s
Output: [10, 'english', 2022.01.01T09:10:01, [a, 21, 3.5]]
s.clear() // Empty the list.
Output: []
Getting Available Attributes and Methods of a List
dir(s)
Output: ['__add__', '__dir__', '__eq__', '__getitem__', '__iadd__', '__imul__', '__init__', '__iter__', '__len__', '__mul__', '__ne__', '__repr__', '__req__', '__rne__', '__str__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort', 'toddb']
toddb
is a unique method in Python Parser, which supports converting
lists in Python Parser into vectors (regular vectors or tuples) in DolphinDB.
s = [1, 2, 3]
type(s.toddb())
Output: dolphindb.VECTOR.INT
s = [[1, 2], `A, 3.2]
type(s.toddb())
Output: dolphindb.VECTOR.ANY
Operators Supported by a List
Operator |
Usage |
Expression |
Output |
---|---|---|---|
* | Duplicate a list | [1, 2, 3] * 3 | [1, 2, 3, 1, 2, 3, 1, 2, 3] |
+ | Join lists | [1, 2022.01M, 3] + [4, 5, "banana"] | [1, 2022.01M, 3, 4, 5, 'banana'] |