Let’s you have a list in Python:
a = [1, 2, 3, 4]
And you have another list in Python:
b = [5, 6, 7, 8]
You can concatenate two lists by simply using + operator, which will leave both lists untouched and return a copy of the concatenated list.
a + b # [1, 2, 3, 4, 5, 6, 7, 8]
You can use extend() method of the array, which allows us to append all the elements from another list to it. This will modify the original list.
# c is None
c = a.extend(b)
# a is now [1, 2, 3, 4, 5, 6, 7, 8]
The append() on the other hand, appends an element to the list. For example,
a = [1, 2, 3, 4]
a.append(5)
# a is now [1, 2, 3, 4, 5]
The append returns None. You can use append to achieve what the extend does.
def extend(a, b):
for x in b:
a.append(x)
–EOF (The Ultimate Computing & Technology Blog) —
197 wordsLast Post: Linear Algorithm to Check If All 1's Are at Least Length K Places Away
Next Post: Algorithm to Check if All Points are On the Same Line