Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Student H
1,473 PointsHow to split a tuple inside a list without splitting the strings that are outside the tuple
My list looks like this: shopping_list = [('apples' , 'oranges') , 'pears'] and I would like to break down the tuple so my list ends up like this ['apples' , 'oranges', 'pears']
When I try to split the items inside the tuple, the console gives an error that tuples object has no split. I tried to make the tuple a list but it also breaks down the string outside the tuple like breaking down 'pears' into 'p' , 'e', 'a', 'r' , 's'. What is the solution for breaking down a tuple in a list without splitting the strings inside the list that are not part of the tuple..
2 Answers
john larson
16,594 PointsThis is as far as I got. I got them separated but don't know what you want to do with them next
>>> for item in shopping_list[0]:
... print(item)
...
apples
oranges
>>>
Student H
1,473 PointsJohn Larson I should give more context on my use case: This is for a shopping list app where the user can add their items either one at a time or add multiple in one line separated by a comma. My problem is when the user adds multiple items separated by a comma, it becomes a tuple mixed with the strings which are a result of the items entered individually. How can we find the items in our list that are tuples and then break them down to mix them with the strings? Having [('apples','oranges'),'pears',('milk','water')] to become: ['apples','oranges,'pears','milk','water']
john larson
16,594 PointsI'm sure (maybe) There's a way to do it. Maybe after separating them add them to a separate list that has separated tuples, appended and the list items appended.