Question

I have this list

[['a', 'a', 'a', 'a'],
 ['b', 'b', 'b', 'b', 'b'],
 ['c', 'c', 'c', 'c', 'c']]

and I want to concatenate 2nd and 3rd elements in each row, starting from the second row, to make something like this:

[['a', 'a', 'a', 'a'],
 ['b', 'bb', 'b', 'b'],
 ['c', 'cc', 'c', 'c']]

It seems to work fine, when I do it to every row:

for index, item in enumerate(list_of_lines, start=0):
    list_of_lines[index][1:3] = [''.join(item[1:3])] 

but when I'm starting from the second row - I have "list index out of range" error:

for index, item in enumerate(list_of_lines, start=1):
    list_of_lines[index][1:3] = [''.join(item[1:3])] 
Was it helpful?

Solution 2

You can explicitly create an iterable with the iter() builtin, then call `next(iterable) to consume one item. Final result is something like this:

line_iter = iter(list_of_lines[:])
# consume first item from iterable
next(line_iter)
for index, item in enumerate(line_iter, start=1):
    list_of_lines[index][1:3] = [''.join(item[1:3])]

Note the slice on the first line, in general it's a bad idea to mutate the thing you're iterating over, so the slice just clones the list before constructing the iterator, so the original list_of_lines can be safely mutated.

OTHER TIPS

When you call

enumerate(list_of_lines, start=1)

, the pairs that it generates are not

1 ['b', 'b', 'b', 'b', 'b']
2 ['c', 'c', 'c', 'c', 'c']

, but rather

1 ['a', 'a', 'a', 'a']
2 ['b', 'b', 'b', 'b', 'b']
3 ['c', 'c', 'c', 'c', 'c']

That is, the start value indicates what the first index used should be, not what the first element to use is.

Perhaps an alternate way of doing this would be as follows:

for (index, item) in list(enumerate(list_of_lines))[1:]:
    list_of_lines[index][1:3] = [''.join(item[1:3])]

There is not much merit here for using enumerate() ... you can simply .pop() the n-th item from inner lists. For loop over your data, starting at index 1 and add the 2nd value (popped) to the 1st element of the inner list:

data = [['a', 'a', 'a', 'a'],
 ['b', 'b', 'b', 'b', 'b'],
 ['c', 'c', 'c', 'c', 'c']] 

for row in range(1,len(data)):  # apply to 1st to n-th inner list by index
    item = data[row].pop(2)         # remove the 2nd item from inner list
    data[row][1] += item            # add it to the 1st of inner list

print(data)

Output:

[['a', 'a', 'a', 'a'], 
 ['b', 'bb', 'b', 'b'], 
 ['c', 'cc', 'c', 'c']]

See list.pop(index)

You can simply do this using list slicing syntax like shown below

for index, item in enumerate(list_of_lines[1:], start=1):
    list_of_lines[index][1:3] = [''.join(item[1:3])]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top