How to split list in a tuple?

split list of tuples into two lists python
tuple to list
split tuple python
split values in a tuple
python unpack tuple
list of tuple unzip
tuple unpacking list comprehension
split items in tuple python

I'm working with stacks, and I want to split the 'push' value. As a whole, this happens to just be a list in a tuple. I got (as output):

('_stack_', ['data inside the stack'])

and I'm looking to get this:

('_stack_',['data','inside','the','stack])

I gave this a try but it threw an error (note: 'Stack' is the variable that refers to the output above):

for i in Stack: 
    Stack[1].split()

If anyone could give me a hand, I assume even though this is a stack the same fundamentals of iterating through a tuple and lists should still hold true? Thanks for the help!

Just create a new tuple with the required values, like this:

t  = ('_stack_', ['data inside the stack'])
nt = (t[0], t[1][0].split())

Now nt is as expected:

nt
=> ('_stack_', ['data', 'inside', 'the', 'stack'])

Unpacking a list / tuple of pairs into two lists / tuples, I want to separate the list in 2 lists. list1 = ('1','2',  Hi, there's a list with tuples. lst = [ ('Meat', 'Milk'), ('Cake - Cookie',), ('Apple', 'Orange')] For each tuple of len == 1 I want to split it at - so it results with: lst = [ ('Meat', 'Milk'), ('Cake', 'Cookie'), ('Apple', 'Orange')] I tried. [tuple(x.split(' - ') for x in tup) for tup in lst if len(tup) == 1]

One liner to create a new tuple using list comprehension

tuple([i[0].split() if isinstance(i,list) else i for i in your_tuple])

Output

('_stack_', ['data', 'inside', 'the', 'stack'])

Python, Python | Split tuple into groups of n. Given a tuple, the task is to divide it into smaller groups of n. initial list (1, 2, 3, 4, 8, 12, 3, 34, 67, 45, 1, 1, 43, 65, 9, 10) Python | Sort tuple list by Nth element of tuple; Python - Convert Tuple String to Integer Tuple; Python - Raise elements of tuple as power to another tuple; Python | Pandas Split strings into two List/Columns using str.split() Python - Convert Tuple to Tuple Pair; Python | Replace tuple according to Nth tuple element; Python - Flatten tuple of List to tuple; Python | Summation of Non-Zero groups

Just hint, not full answer - maybe its full answer, because your main problem seems you cant split the string.... : You are trying to split list, you need to split string. Tuple is immutable, so you need to transform to list and back to tuple, or make new tuple from original tuple elements

splitted_list = ('_stack_', ['data inside the stack'])[1][0].split()

How to split Python tuples into sub-tuples?, The result will be a list of 4 tuples each with 3 numbers. >>> tup=(7,6,8,  Typecasting to tuple can be done by simply using tuple (list_name). A small variation to the above approach is to use a loop inside tuple () . This essentially unpacks the list l inside a tuple literal which is created due to the presence of the single comma (, ).

Lists and Tuples in Python – Real Python, You'll cover the important characteristics of lists and tuples in Python 3. You'll learn how to define them and how to manipulate them. When you're finished, you​  Example: Convert Python Tuple to List. In the following example, we initialize a tuple with all integers and convert it to a list using list(sequence).

Lists and tuples, If the argument is a sequence (string, list or tuple), the result is a tuple with the The return value from split is a list with two elements; the first element is  Lists and tuples are arguably Python’s most versatile, useful data types.You will find them in virtually every nontrivial Python program. Here’s what you’ll learn in this tutorial: You’ll cover the important characteristics of lists and tuples.

3. Strings, lists, and tuples, In the case of lists or tuples, they are made up of elements, which are values of any The split method invoked on a string and separates the string into a list of  Our task is to convert the given string to a tuple. We can use the built-in string method split() to fetch each of the comma-separated string into a list and then convert it to a tuple. Syntax of split(): inputstring.split(seperator=None, maxsplit=-1) It uses the separator as the delimiter string and returns a list of words from the string.

Comments
  • tuple are not mutable so you can't change the list in the tuple, you will need to recreate a new tuple. You don't need the for loop (you aren't using i). e.g. tuple(Stack[0], Stack[1][0].split()
  • This feels like an XY problem.