-1

I have some lists:

list_line[0] = ['my name is A']
list_line[0] = ['my name is B']
list_line[0] = ['my name is C']
list_line[0] = ['my name is D']

And I make this work:

list_words_0 = list_line[0].split()
list_words_1 = list_line[1].split()
list_words_2 = list_line[2].split()
list_words_3 = list_line[3].split()

list_words = list_words_0 + list_words_1 + list_words_2 + list_words_3
print list_words

So with the above steps, i can do it with a for-loop: for i in range(3): do something

but i don't know how to put i in variables: list_words_0, list_words_1, list_words_2 ... Can you help me change the name of that lists with a for loop? thank you so much !

SWdream
  • 215
  • 2
  • 3
  • 10
  • It's not *some* lists, you have a list containing one nested list – vaultah Apr 12 '14 at 05:13
  • Please post **correct** code and consider telling us what you are trying to accomplish, because it is not clear at all from what you have posted. – Two-Bit Alchemist Apr 12 '14 at 05:14
  • hi @shaktimaan! Thanks for your answer. But i want to parse that lists into the lists like this: list_line[0] = ['my', 'name', 'is', 'A'], list_line[1] = ['my', 'name', 'is', 'B'], list_line[2] = ['my', 'name', 'is', 'C'], list_line[4] = ['my', 'name', 'is', 'D']. And that is particular lists – SWdream Apr 14 '14 at 02:19

2 Answers2

1

I'd recommend using Dictionaries. What you want to do is loop 0 to i (i being the max amount of list_words_ variables you want), then create a string that goes "list_words_" + str(i). Then use the dictionary to keep track of these:

#assume you're inside the loop
example_dictionary["list_words_" + str(i)] = list_line[0];

NOTE: Treat this as pseudo code. It's kind of late so I may have small syntax errors.

However, your question is a bit unclear. It may be that you want to combine multiple lists or arrays into one list or array. There are many examples of this already on this site (like this and this).

Community
  • 1
  • 1
0

I am assuming your data is like this since you are trying to do a split later on:

list_line = ['my name is A', 
 'my name is B', 
 'my name is C', 
 'my name is D']

Your end goal (of concatenating all the lists of words) can be achieved by simply doing:

output_list = []
for alist in list_line:
    output_list.append(alist.split())

prints

 [['my', 'name', 'is', 'A'],
 ['my', 'name', 'is', 'B'],
 ['my', 'name', 'is', 'C'],
 ['my', 'name', 'is', 'D']]
shaktimaan
  • 11,962
  • 2
  • 29
  • 33
  • hi @shaktimaan! Thanks for your answer. But i want to parse that lists into the lists like this: list_line[0] = ['my', 'name', 'is', 'A'], list_line[1] = ['my', 'name', 'is', 'B'], list_line[2] = ['my', 'name', 'is', 'C'], list_line[4] = ['my', 'name', 'is', 'D']. And that is particular lists – SWdream Apr 14 '14 at 02:23
  • @user3208948 Change `extend` to `append` to get that. Updated my answer – shaktimaan Apr 14 '14 at 02:30