Convert Tuple To List In A Dictionary
I have a dictionary like this: a= {1982: [(1,2,3,4)], 1542: [(4,5,6,7), (4,6,5,7)]} and I want to change the all the tuples (1,2,3,4),(4,5,6,7),(4,6,5,7) to lists,
Solution 1:
As far as I understand you want to convert each tuple in a list. You can do this using a dictionary comprehension:
{k: [list(ti) for ti in v] for k, v in a.items()}
will give
{1542: [[4, 5, 6, 7], [4, 6, 5, 7]], 1982: [[1, 2, 3, 4]]}
Is that what you are after?
Solution 2:
In-place:
for key, value in a.items():
for i, t in enumerate(value):
value[i]= list(t)
New objects:
{key: [list(t) for t in value] for key, value in a.items()}
Solution 3:
You can use "tupleo" library
from tupleo import tupleo.
val = tupleo.tupleToList(a[1542]).
print(val) [[4,5,6,7], [4,6,5,7]]
tupleo gives you full depth level conversion of tuple to list. and it have functionality to convert tuple to dict also based on index.
Post a Comment for "Convert Tuple To List In A Dictionary"