Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert element in Python list after every nth element

Say I have a Python list like this:

letters = ['a','b','c','d','e','f','g','h','i','j'] 

I want to insert an 'x' after every nth element, let's say three characters in that list. The result should be:

letters = ['a','b','c','x','d','e','f','x','g','h','i','x','j'] 

I understand that I can do that with looping and inserting. What I'm actually looking for is a Pythonish-way, a one-liner maybe?

like image 230
QuestionEverything Avatar asked Jun 25 '15 03:06

QuestionEverything


People also ask

How do you add an element after an element in Python?

The Python list data type has three methods for adding elements: append() - appends a single element to the list. extend() - appends elements of an iterable to the list. insert() - inserts a single item at a given position of the list.

How do you add items to the middle of a list in Python?

insert() Method. Use the insert() method when you want to add data to the beginning or middle of a list. Take note that the index to add the new element is the first parameter of the method.


2 Answers

I've got two one liners.

Given:

>>> letters = ['a','b','c','d','e','f','g','h','i','j'] 
  1. Use enumerate to get index, add 'x' every 3rd letter, eg: mod(n, 3) == 2, then concatenate into string and list() it.

    >>> list(''.join(l + 'x' * (n % 3 == 2) for n, l in enumerate(letters)))  ['a', 'b', 'c', 'x', 'd', 'e', 'f', 'x', 'g', 'h', 'i', 'x', 'j'] 

    But as @sancho.s points out this doesn't work if any of the elements have more than one letter.

  2. Use nested comprehensions to flatten a list of lists(a), sliced in groups of 3 with 'x' added if less than 3 from end of list.

    >>> [x for y in (letters[i:i+3] + ['x'] * (i < len(letters) - 2) for      i in xrange(0, len(letters), 3)) for x in y]  ['a', 'b', 'c', 'x', 'd', 'e', 'f', 'x', 'g', 'h', 'i', 'x', 'j'] 

(a) [item for subgroup in groups for item in subgroup] flattens a jagged list of lists.

like image 148
Mark Mikofski Avatar answered Sep 21 '22 13:09

Mark Mikofski


Try this

i = n while i < len(letters):     letters.insert(i, 'x')     i += (n+1) 

where n is after how many elements you want to insert 'x'.

This works by initializing a variable i and setting it equal to n. You then set up a while loop that runs while i is less then the length of letters. You then insert 'x' at the index i in letters. Then you must add the value of n+1 to i. The reason you must do n+1 instead of just n is because when you insert an element to letters, it expands the length of the list by one.

Trying this with your example where n is 3 and you want to insert 'x', it would look like this

letters = ['a','b','c','d','e','f','g','h','i','j'] i = 3 while i < len(letters):     letters.insert(i, 'x')     i += 4  print letters 

which would print out

['a', 'b', 'c', 'x', 'd', 'e', 'f', 'x', 'g', 'h', 'i', 'x', 'j'] 

which is your expected result.

like image 24
michaelpri Avatar answered Sep 20 '22 13:09

michaelpri