Reputation: 447
I am trying to return a list of tuples in the below
def lambda_handler(event, context):
t1 = [('a', 'string', 'a', 'string'), ('b', 'string', 'b', 'string')]
print(t1)
return t1
When I print
the list or convert the list to str
-> print
, I get the below:
[('a', 'string', 'a', 'string'), ('b', 'string', 'b', 'string')]
When I return
I get
[
[
"a",
"string",
"a",
"string"
],
[
"b",
"string",
"b",
"string"
]
]
My challenge is that I need to submit a list of tuples for an AWS Glue Job service:
ApplyMapping.apply(frame = datasource0, mappings = [("a", "string", "a", "string"), ("b", "string", "b", "string")], transformation_ctx = "applymapping1")
But I pass (MY_LIST_PARAM) which is the output of the return
command:
ApplyMapping.apply(frame = datasource0, mappings = MY_LIST_PARAM, transformation_ctx = "applymapping1")
However, I then get the following error:
TypeError: Mapping must be specified as a tuple. Got [
I can't hard code the values, as it is a single job which take the argument list and applies that in the job logic. Printing the List as a String doesn't help me (even though it looks correct) as I need a list of tuples in format [(),()]
and not [[],[]]
.
I am using Python 3.7. Also executing in AWS Lambda (but this should not make a difference)
Upvotes: 2
Views: 1542
Reputation: 42237
Also executing in AWS Lambda (but this should not make a difference)
I expect it does. I'd guess the data is moved from lambda to glue (or whatever) as JSON[0], JSON doesn't have tuples so python converts tuples to JSON arrays, which become lists on the other side.
Convert the lists back to tuples on the receiver side if the API you're calling throws a fit when given lists. It's just a matter of running a list comprehension or list(map(tuple, MY_LIST_PARAM))
.
[0] or some other format with similar capabilities / limitations I can't think of any serialisation format which would provide for Python's distinction between tuples and lists which is only a matter of mutability (and thus not really a concern to serialisation formats), they might have something like an "anoymous structure" or "anonymous record" but Python tuples would rarely get serialized to that, at least implicitly
Upvotes: 5