python append list elements to list code example

Example 1: add item to list python

list.append(item)

Example 2: append to lists python

list = ['larry', 'curly', 'moe']
  list.append('shemp')         ## append elem at end
  list.insert(0, 'xxx')        ## insert elem at index 0
  list.extend(['yyy', 'zzz'])  ## add list of elems at end
  print list  ## ['xxx', 'larry', 'curly', 'moe', 'shemp', 'yyy', 'zzz']
  print list.index('curly')    ## 2

  list.remove('curly')         ## search and remove that element
  list.pop(1)                  ## removes and returns 'larry'
  print list  ## ['xxx', 'moe', 'shemp', 'yyy', 'zzz']

Example 3: append to lists python

list = []          ## Start as the empty list
  list.append('a')   ## Use append() to add elements
  list.append('b')

Example 4: how to append list in python

list1 = ["hello"]
list1 = list1 + ["world"]

Example 5: python add all values of another list

a = [1, 2, 3]
b = [4, 5, 6]
a += b
# another way: a.extend(b)

Example 6: append list python

my_list = ['a', 'b', 'c']
my_list.append('e')
print(my_list)
# Output
#['a', 'b', 'c', 'e']