li=['a', 'b', 'mpilgrim', 'z', 'example']
li.append("new")
li.insert(2, "new")
li.extend(["two", "elements"]): concatenates li list wth a list
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']
li = li + ['example', 'new']: the same result as list.extend(otherlist). But the + operator returns a new (concatenated) list as a value
li += ['two'] # equivalent to li.extend(['two'])
li = [1, 2] * 3 # repeater. li = [1, 2] * 3 is equivalent to li = [1, 2] + [1, 2] + [1, 2]
The Difference between extend and append
extend takes a single argument, which is always a list, and adds each of the elements of that list to the original list.
li.extend(['d', 'e', 'f']). #Result: ['a', 'b', 'c', 'd', 'e', 'f']
append takes one argument, which can be any data type,and simply adds it to the end of the list as an element
li.append(['d', 'e', 'f']) #Result: ['a', 'b', 'c', ['d', 'e', 'f']]