Reputation:
Is it possible to insert to a list when a specific string appears. Example:
List=['north','south','east','west','south','united']
So for every time the string 'south' is presented the list will be insert an item 'canada'
before the element south in the list.
Results
List=['north','canada','south','east','west','canada','south','united']
EDIT: I apologize for not being specific enough. I need it to seek the specific characters.
List1=['north','<usa>23<\usa>','east','west','<usa>1942<\usa>','united']
Result=['north','canada', '<usa>23<\usa>','east','west','canada','<usa>1942<\usa>','united']
Upvotes: 10
Views: 40504
Reputation: 170
Inserting element before another
>>> x = [1,2,3,4,5]
>>> x.insert(x.index(4), 22)
>>> x
[1, 2, 3, 22, 4, 5]
Inserting element after another
>>> x = [1,2,3,4,5]
>>> x.insert(x.index(3)+1, 22)
>>> x
[1, 2, 3, 22, 4, 5]
However, this does not work for multiple insertions
>>> x = [1,2,3,4,5,4,6,4,7]
>>> x.insert(x.index(4), 22)
>>> x
[1, 2, 3, 22, 4, 5, 4, 6, 4, 7]
Upvotes: 9
Reputation: 1089
The simple answer:
def blame_canada(old_list):
new_list = []
for each in old_list:
if each == 'south':
new_list.append('canada')
new_list.append(each)
return new_list
Upvotes: 5
Reputation: 85452
Use a simple loop:
L = ['north','south','east','west','south','united']
res = []
for entry in L:
if entry == 'south':
res.append('canada')
res.append(entry)
L[:] = res
The last line copies the result into the original list. This makes it equivalent to append
that modifies the original list. Using a lot of insert
calls would make it really slow for large list as it need to build the whole lists for each insert.
Upvotes: 3
Reputation: 16325
Here's a high-performance list-comprehension method for doing this.
First, it generates a list of indexes containing "south".
Then, it uses the index + index-order for the insertion point (to account for previous insertions in the loop), making for a simple, yet quick and easy-to-follow pythonic solution for this problem:
List = ['north','south','east','west','south','united']
souths = [idx for idx, val in enumerate(List) if val == 'south']
for i, idx in enumerate(souths):
List.insert(idx+i, 'canada')
Upvotes: 1
Reputation: 476659
You can use a simple one-liner using double list comprehension (using List
like you defined it):
>>> [inner for outer in [(['canada',x] if x == 'south' else [x]) for x in List] for inner in outer]
['north', 'canada', 'south', 'east', 'west', 'canada', 'south', 'united']
The answers consists out of two parts:
the inner list comprehension: [(['canada',x] if x == 'south' else [x]) for x in List]
this iterates over the list and returns a list of lists. In case x
is south, it emits ['canada','south']
, otherwise it emits [x]
. So the result is something like:
>>> [(['canada',x] if x == 'south' else [x]) for x in List]
[['north'], ['canada', 'south'], ['east'], ['west'], ['canada', 'south'], ['united']]
for the given list.
Next the result is flattened using this approach.
Upvotes: 3
Reputation: 6360
To be able to place elements within a list in specific positions, you need to list insert
function.
<list>.insert(<position>, <value>)
As an example, if you have a list containing the following:
a = [2, 4, 6, 8, 12]
And want to add 10 in the list but maintain the current order, you can do:
a.insert(4, 10)
and will get:
[2, 4, 6, 8, 10, 12]
EDIT
To insert before every 'south' do:
for i in range(len(<list>)):
if <list>[i] == 'south':
<list>.insert(i, 'canada')
Upvotes: 13