Reputation: 11
I'm trying to turn this:
('a','b',['a1','b1','b3',('a2',('ab','bd','cd'),'b2','c2')])
into this:
('a','b',['a1','b1','b3',('a2',['ab','bd','cd'],'b2','c2')])
changing the ('ab', 'bd', 'cd') to ['ab', 'bd', 'cd']
Upvotes: 1
Views: 97
Reputation: 2670
You can use the list()
function:
tup = ('ab', 'bd', 'cd')
lst = list(tup)
print(lst)
Output:
['ab', 'bd', 'cd']
EDIT:
If you want to get your output, it is a bit more complicated since tuples are immutable (unchangable), so we need to create a new tuple that stores the new change:
origTup = ('a','b',['a1','b1','b3',('a2',('ab','bd','cd'),'b2','c2')])
origLst = list(origTup) #convert origTup to a list so we can edit it
partialLst = list(origLst[2][3]) #extract ('a2',('ab','bd','cd'),'b2','c2') and change it to a list
partialLst[1] = list(partialLst[1]) #change ('ab','bd','cd') to a list
partialTup = tuple(partialLst) #convert ['a2',['ab','bd','cd'],'b2','c2'] back to a tuple
origLst[2][3] = partialTup #put the tuple back into our origLst
newTup = tuple(origLst) #create a new tuple that converts our origLst to a tuple
print(newTup)
Output:
('a', 'b', ['a1', 'b1', 'b3', ('a2', ['ab', 'bd', 'cd'], 'b2', 'c2')])
First, we convert origTup
to a list, origLst
so we can edit it. Then, we will extract ('a2',('ab','bd','cd'),'b2','c2')
from our list and change it to a list: ['a2',('ab','bd','cd'),'b2','c2']
Now that we can edit it, we will change the first element, ('ab','bd','cd')
, to a list: ['ab','bd','cd']
. Then, we will again change the outer portion into a tuple as it originally was: ['a2',['ab','bd','cd'],'b2','c2']
into ('a2',['ab','bd','cd'],'b2','c2')
Finally, we set the element in our origLst
to this tuple, and then create a new tuple to store our change.
I hope this helped! Please let me know if you need any further help or clarification!
Upvotes: 2