Reputation: 129
I am trying to convert this tuple into a list, however when I run this code:
mytuple=('7578',), ('6052',), ('8976',), ('9946',)
List=[]
for i in mytuple:
Start,Mid,End = map(str, mytuple.split("'"))
List.append(Mid)
print(List)
I receive this error:
AttributeError: 'tuple' object has no attribute 'split'
The output should be:
[7578, 6052, 8976, 9946]
Upvotes: 5
Views: 16551
Reputation: 530970
I would use itertools.chain.from_iterable
(which errs on the side of too much verbosity than too little):
from itertools import chain
result = [int(x) for x in chain.from_iterable(mytuple)]
# vs ... for x, in mytuple]; the comma is easy to miss
Somewhere between the two extremes would be
result = [int(x) for x in chain(*mytuple)]
Upvotes: 1
Reputation: 6429
this is what you are looking for
mytuple = (('7578',), ('6052',), ('8976',), ('9946',))
result = [int(x) for x, in mytuple]
print(result)
Upvotes: 9
Reputation: 61910
If I understood correctly, this is want you want:
mytuple = ('7578',), ('6052',), ('8976',), ('9946',)
result = [e for e, in mytuple]
print(result)
Output
['7578', '6052', '8976', '9946']
Upvotes: 4