Ask your own question, for FREE!
Computer Science 13 Online
OpenStudy (anonymous):

Python Question Is there a way to split a tuple into different variables. i.e. male_names and female_names ['John','Sarah'] I have a bunch of tuples with girls and guys names from a regular expression and I need to use them separately.

OpenStudy (asnaseer):

if you had, say: names=['John', 'Sarah'] then you could just use: male=names[0] female=names[1]

OpenStudy (asnaseer):

or do you mean your tuple is more like: names=['male1', 'female1', 'male2', 'female2', ...]

OpenStudy (anonymous):

yea my tuple is more like your second example.

OpenStudy (asnaseer):

ok, then you could use list comprehensions as follows: names=['male1', 'female1', 'male2', 'female2', ...] male_names = [names[i] for i in range(0, len(names)) if i % 2 == 1] female_names = [names[i] for i in range(0, len(names)) if i % 2 == 1]

OpenStudy (anonymous):

thanks that works

OpenStudy (asnaseer):

yw

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!