0

For example:

list_string = ["This is a string that I want."
, "this is also a string that I want!"
, "and this"
, "and also this one!!"]

and what I would wish to obtain is:

list_string1 = ["This is a string that I want."]
list_string2 = ["this is also a string that I want!"]
list_string3 = ["and this"]
list_string4 = ["and also this one!!"]

I am aware that there are many questions asked regarding the splitting of strings in python in stackoverflow however I have not found one with the answer to my question here. Is this even possible?

Would really appreciate if someone could give me some input on this as I genuinely want to gain some knowledge!

Ckoh
  • 51
  • 5
  • 1
    The real answer to your question is "Don't do it, keep your data as a list". A list is far easier to work with than a variable number of variables. – Aran-Fey Nov 04 '17 at 11:14
  • When you start adding numbers to variable names, you need to use a list instead. – Code-Apprentice Nov 04 '17 at 13:13

3 Answers3

0

Check this

>>> list_string = ["This is a string that I want."
, "this is also a string that I want!"
, "and this"
, "and also this one!!"]
>>> [[i] for i in list_string]
[['This is a string that I want.'], ['this is also a string that I want!'], ['and this'], ['and also this one!!']]
>>> 

You can call individual lists using list_string[0],list_string[1] so on

Sandeep Lade
  • 1,865
  • 2
  • 13
  • 23
  • Hey @Sandeep Lade, I would like to clarify how does adding [i] before the line of the "for" loop result in the production of the lists of strings? I do not understand the concept. I understand that using "for i in list_string" will iterate over the characters. But how does including the [i] change the output? I do not understand the concept basically. – Ckoh Nov 04 '17 at 05:46
  • 1
    @Ckoh : This is called list comprehension . You can iterate over all values in a list in one line of code . Read more about list comprehension http://www.pythonforbeginners.com/basics/list-comprehensions-in-python – Sandeep Lade Nov 04 '17 at 05:48
0

What you want to do is called 'destructuring'.

You can do it in this simple way:

>>> list_string1, list_string2, list_string3, list_string4 = list_string
>>> list_string1
'This is a string that I want.'
J.M. Robles
  • 614
  • 5
  • 9
0

try this:

lists=[[line] for line in list_string]
for i in range(len(lists)):
    print 'list_string'+ str(i+1 )+' =' ,lists[i]
Samsul Islam
  • 2,581
  • 2
  • 17
  • 23