0

I currently have one list with x amount of items (number of items may change each time I use the program).

Example:

['This is in index 0, This is in index 0, This is in index 0, This is in index 0' , 
'This is in index 1, This is in index 1, This is in index 1']

I would like to be able to split each index into different lists and have each index of the newly created lists separated by the comma to produce something like below.

['This was in index 0,' 'This was in index 0,' 'This was in index 0,' 'This was in index 0'] 
['This was in index 1,' 'This was in index 1,' 'This was in index 1']

This is the my first ever attempt at writing anything useful so any help and explanations would be hugely appreciated.

Sayse
  • 42,633
  • 14
  • 77
  • 146
Dean1886
  • 1
  • 4

4 Answers4

2

You can do this:

input_list = ["this is index 0, this is index 0, this is index 0", "this is index 1, this is index 1, this is index 1"]
output_list = []

for i in input_list:
    split_string = i.split(",")
    output_list.append(split_string)

print(output_list)

EDIT: This is iterating over the original list, splitting each index by a comma, then adding the resultant list to the 'output_list', which is a list containing two lists, each nested list is the split 'this is index X' list.

Michael
  • 153
  • 1
  • 9
0

One of the simpler ways would be the use of the sublist syntax

name_of_list[start:end:step]

As such, if we pretend you know there are 4 0's and 3 1's, you could try:

ohs = 4
ones = 3
only0s = my_list[:ohs] #when you neglect the first value, it's assumed 0
only1s = my_list[ohs:] #when you leave out the last value, it's assumed len(list)

Alternatively, you can use negatives to represent from the end, so...

backOhs = my_list[:-ones]
backOnes =my_list[-ones:]

EDIT: as an aside, this also works with lists, so...

my_word = 'hello'
print(my_word[2:4])

Prints 'll'

c4llmeco4ch
  • 311
  • 2
  • 11
0

You can use list comprehension to separate the data like this:

original = ['This is in index 0, This is in index 0, This is in index 0, This is in index 0' ,'This is in index 1, This is in index 1, This is in index 1']

list_zero = [x for x in original if '0' in x]

The result would be :

['This is in index 0, This is in index 0, This is in index 0, This is in index 0']

Then repeat for list one:

list_one = [x for x in original if '1' in x]

Here is a website you can use to learn more about list comprehensions.

Nick
  • 3,454
  • 6
  • 33
  • 56
0

you can use this code:

main_list= ['This is in index 0, This is in index 0, This is in index 0, This is in 
index 0' , 'This is in index 1, This is in index 1, This is in index 1']
matrix=[]
for item in main_list:
    sub_list=item.split(",")
    matrix.append(sub_list)

and for output you can call this :

print(matrix[0])
print(matrix[1])
>>>['This is in index 0', ' This is in index 0', ' This is in index 0', ' This is in index 0']
>>>['This is in index 1', ' This is in index 1', ' This is in index 1']
Benyamin Karimi
  • 133
  • 1
  • 1
  • 9