Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace the last element in a list with another list

Tags:

python

For example I have two arrays a array1 and array2

array1 = ['A', 'B', 'C', 'D', 'E', 'F',]
array2 = ['G', 'H', 'I',]`

Now I want the output as

array1 = ['A', 'B', 'C', 'D', 'E', 'G', 'H', 'I',]

How can I do this in python

like image 677
user1275375 Avatar asked Jul 09 '12 10:07

user1275375


People also ask

How do you replace an item in a list with another list in Python?

We can replace values inside the list using slicing. First, we find the index of variable that we want to replace and store it in variable 'i'. Then, we replace that item with a new value using list slicing.

Does replace () work on lists?

You can replace items in a Python list using list indexing, a list comprehension, or a for loop. If you want to replace one value in a list, the indexing syntax is most appropriate.

How do you use replace in a list?

Replace a specific string in a list. If you want to replace the string of elements of a list, use the string method replace() for each element with the list comprehension. If there is no string to be replaced, applying replace() will not change it, so you don't need to select an element with if condition .


1 Answers

>>> array1 = ['A', 'B', 'C', 'D', 'E', 'F']
>>> array2 = ['G', 'H', 'I']
>>> array1 = array1[:-1] + array2
>>> array1
['A', 'B', 'C', 'D', 'E', 'G', 'H', 'I']
like image 122
jamylak Avatar answered Nov 15 '22 15:11

jamylak