I everyone,
I struggle to convert an array of array like this :
[ ["foo","bar","foo","bar","foo"], ["foo","bar","foo","bar","foo"], ["foo","bar","foo","bar","foo"], ["foo","bar","foo","bar","foo"] ]
to an array of tuples (array
, str
) like this:
[ (["foo","bar","foo","bar","foo"], "type1"), (["foo","bar","foo","bar","foo"], "type1"), (["foo","bar","foo","bar","foo"], "type1"), (["foo","bar","foo","bar","foo"], "type1") ]
I did find a way to append the type to the array but it’s not exactly what I want:
[ ["foo","bar","foo","bar","foo", "type1"], ["foo","bar","foo","bar","foo", "type1"], ["foo","bar","foo","bar","foo", "type1"], ["foo","bar","foo","bar","foo", "type1"] ]
Do you have something better ? Thanks :)
Advertisement
Answer
Solution
Shortest solution: list(zip(vals, types))
🔥🔥🔥
vals = [ ["foo","bar","foo","bar","foo"], ["foo","bar","foo","bar","foo"], ["foo","bar","foo","bar","foo"], ["foo","bar","foo","bar","foo"] ] # If you must specify different types for each element # uncomment the following line # types = ['type1', 'type2', 'type3', 'type4'] # If all of them should have the same type types = ['type1' for _ in range(len(vals))] # Finally combine vals and types list(zip(vals, types))
Output:
[(['foo', 'bar', 'foo', 'bar', 'foo'], 'type1'), (['foo', 'bar', 'foo', 'bar', 'foo'], 'type1'), (['foo', 'bar', 'foo', 'bar', 'foo'], 'type1'), (['foo', 'bar', 'foo', 'bar', 'foo'], 'type1')]