-1

I have the user put in 2 words and then I want to put the 2 words in 2 different lists, like:

ask=input("write 2 words here:") Hello world

list1 = ['Hello']
list2 = ['world']
tobias_k
  • 81,265
  • 12
  • 120
  • 179
G U I
  • 17
  • 4
  • 1
    Does this answer your question? [How to split a string into a list?](https://stackoverflow.com/questions/743806/how-to-split-a-string-into-a-list) – Lescurel Oct 27 '20 at 13:54

3 Answers3

2

A possible solution would be:

word1, word2 = input().split()
list1, list2 = [word1], [word2]
Lorena Gil
  • 341
  • 1
  • 6
  • This code only works when you enter two words. Is it possible that in your case you are entering more than two words? – Lorena Gil Oct 28 '20 at 09:17
0

so if you want the user to put any number of words, then I suggest using one list containing all other lists. for example:

user_input = input("write some words here:").split()
WordList = []
for word in user_input:
    WordList.append( [word] )
print(WordList)

this code creates a list of lists. for recalling any list that you want all you have to do is to use its index like print( WordList[2] )

Shoaib Mirzaei
  • 512
  • 4
  • 11
  • im trying not to have the user have to put in 2 input words – G U I Oct 27 '20 at 20:11
  • @GUI I changed the answer, see if it's what you wanted. but if you are trying to learn python, I suggest you try coding using trial and error method, that's exactly how you master your coding skill – Shoaib Mirzaei Oct 28 '20 at 05:03
  • i have been editing here and there but its like the code does nothing since there is no error, nothing apears – G U I Oct 28 '20 at 08:44
0
word_list = []
ask = input("write n words here:") Hello world
split_list = ask.split()
for word in split_list:
    word_List.append([word])

#input: hello what is this
print(len(word_list))
# 4

print(word_list)
[['hello'],['what'],['is'],['this']]

# You can access individual word list by their index
print(word_list[0])
# hello


  • is there a way to like do this depending on how may words the user putsin and not just 2? – G U I Oct 27 '20 at 21:11
  • Since the earlier answer was based on 2 words (that's what your original question was). I have modified the same to 'n-words` version. Hope this helps. – Rohit Kewalramani Oct 28 '20 at 05:01