Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split list into different variables

Tags:

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')
like image 957
Eagle Avatar asked Feb 14 '14 23:02

Eagle


People also ask

How do you split values in a list?

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() .

How do I split a string into multiple variables?

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.

Can you split () a list in Python?

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.


1 Answers

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') 
like image 195
Martijn Pieters Avatar answered Sep 30 '22 22:09

Martijn Pieters