I have a list like this:
[('love', 'yes', 'no'), ('valentine', 'no', 'yes'), ('day', 'yes','yes')]
How do I split this list into three variables with each variable holding respectively
('love', 'yes', 'no')
('valentine', 'no', 'yes')
('day', 'yes','yes')
split() method splits the string into a list of substrings using a delimiter. We passed 1 for the maxsplit argument to only split the string once and get the first item. If you need to split the string on each occurrence of the delimiter, remove the second argument in the call to str. split() .
The str. split() method will split the string into a list of strings, which can be assigned to variables in a single declaration. Copied! The example splits the string into a list of strings on each space, but you could use any other delimiter.
Python String split() MethodThe split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.
Assign to three names:
var1, var2, var3 = listobj
Demo:
>>> listobj = [('love', 'yes', 'no'), ('valentine', 'no', 'yes'), ('day', 'yes','yes')] >>> var1, var2, var3 = listobj >>> var1 ('love', 'yes', 'no') >>> var2 ('valentine', 'no', 'yes') >>> var3 ('day', 'yes', 'yes')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With