Option 1
Use map(short and sweet, but slow):
l_2 = list(map(lambda x: tuple(x.values()), l))
In the function, lambdaspecify that you want to create a tuple of values dict. This can also be accelerated by using the vanilla function instead lambda:
def foo(x):
return tuple(x.values())
l_2 = list(map(foo, l))
print(l_2)
[
('39.99', 'cat', 'True'),
('67.99', 'cat', 'True'),
('67.00', 'dog', 'False'),
('11.28', 'fish', 'True')
]
Option 2
Use a list comprehension. Another answer has already given a solution for this.
, .items() .values():
new_list = []
for x in l:
new_list.append(tuple(x.values()))
print(new_list)
[
('39.99', 'cat', 'True'),
('67.99', 'cat', 'True'),
('67.00', 'dog', 'False'),
('11.28', 'fish', 'True')
]
, .
Solution 1 (improved): 100000 loops, best of 3: 2.47 µs per loop
Solution 2: 1000000 loops, best of 3: 1.79 µs per loop
Your solution: 100000 loops, best of 3: 2.03 µs per loop