1

I have a list of lists of strings:

listBefore = [['4', '5', '1', '1'],
              ['4', '6', '1', '1'],
              ['4', '7', '8', '1'],
              ['1', '2', '1', '1'],
              ['2', '3', '1', '1'],
              ['7', '8', '1', '1'],
              ['7', '9', '1', '1'],
              ['2', '4', '3', '1']]

and I would like to add a counter/index in the middle of each list so that the list looks like:

listAfter = [['4', '5', '1', '1', '1'],
             ['4', '6', '2', '1', '1'],
             ['4', '7', '3', '8', '1'],
             ['1', '2', '4', '1', '1'],
             ['2', '3', '5', '1', '1'],
             ['7', '8', '6', '1', '1'],
             ['7', '9', '7', '1', '1'],
             ['2', '4', '8', '3', '1']]

What would be the easiest way to do so? I could probably loop over the lists and add the index, but is there a cleaner way to do so?

Cheers, Kate

Edit: The code I wrote works for me:

item = 1
for list in listBefore:
    list.insert(2,str(item))
    item = item + 1
print listBefore

I was wondering if there is another way to do so more efficiently or in one step.

2
  • 1
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the FAQ and How to Ask. Commented Oct 5, 2016 at 4:11
  • I agree you should try writing some yourself and see what works. For some help, check out enumerate. Commented Oct 5, 2016 at 4:17

4 Answers 4

2

Iterate over the parent list with enumerate() to get counter (used as i in the below example) along with list element. In the sublist, insert the i at the middle of sublist using list.insert(index, value) method. Note: The value of counter i.e. i will be of int type, so you have to explicitly type cast it to str as str(i)before inserting. Below is the sample code:

for i, sub_list in enumerate(my_list, 1):  # Here my_list is the list mentioned in question as 'listBefore'
    sub_list.insert(len(sub_list)/2, str(i))

# Value of 'my_list'
# [['4', '5', '1', '1', '1'], 
#  ['4', '6', '2', '1', '1'], 
#  ['4', '7', '3', '8', '1'],
#  ['1', '2', '4', '1', '1'], 
#  ['2', '3', '5', '1', '1'], 
#  ['7', '8', '6', '1', '1'], 
#  ['7', '9', '7', '1', '1'], 
#  ['2', '4', '8', '3', '1']]
Sign up to request clarification or add additional context in comments.

Comments

1

You can perform this with a list comprehension

listAfter = [listBefore[i][:len(listBefore[i])/2] + [str(i+1)] + listBefore[i][len(listBefore[i])/2:] for i in range(len(listBefore))]

Comments

0

You should learn about enumerate, it lets you iterate over the list with two iterators - one (str_list in this case) holds the current item in the list, and the other (i`) holds it's index in the list.

for i,str_list in enumerate(listBefore):
  listBefore[i] = str_list[:len(str_list)//2] + [str(i+1)] + str_list[len(str_list)//2:]

Comments

0
>>> data = [['4', '5', '1', '1'],
            ['4', '6', '1', '1'],
            ['4', '7', '8', '1'],
            ['1', '2', '1', '1'],
            ['2', '3', '1', '1'],
            ['7', '8', '1', '1'],
            ['7', '9', '1', '1'],
            ['2', '4', '3', '1']]
>>> print [row[:len(row)//2] + [str(i)] + row[len(row)//2:]
           for i, row in enumerate(data, start=1)]
[['4', '5', '1', '1', '1'],
 ['4', '6', '2', '1', '1'],
 ['4', '7', '3', '8', '1'],
 ['1', '2', '4', '1', '1'],
 ['2', '3', '5', '1', '1'],
 ['7', '8', '6', '1', '1'],
 ['7', '9', '7', '1', '1'],
 ['2', '4', '8', '3', '1']]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.