adding lists in python code example

Example 1: python add to list

list_to_add.append(item_to_add)

Example 2: add item to list python

list.append(item)

Example 3: how to add a new element to a list in python

#!/usr/bin/env python

# simple.py

nums = [1, 2, 3, 4, 5]

nums.append(6)

Example 4: python push to list

append(): append the object to the end of the list.
insert(): inserts the object before the given index.
extend(): extends the list by appending elements from the iterable.

Example 5: add list python

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

Example 6: add a list in python

list_of_names=["Bill", "John", "Susan", "Bob", "Emma","Katherine"]
new_name="James"
list_of_names.append(new_name)
# The list is now ["Bill", "John", "Susan", "Bob", "Emma","Katherine", "James"]

Tags:

Java Example