Python List append() Method with Examples

In the previous article, we have discussed Python String islower() Method with Examples
List in Python:

Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.

List append() Method in Python:

The append() method adds an element or item to the end of the list.

Syntax:

list.append(item)

Parameters

The method only accepts one argument.

item – an item (number, string, list, etc.) that will be appended to the end of the list

Return Value:

The append() method produces no output (returns None).

Examples:

Example1:

Input:

Given list = ["hello", "this", "is"]
Item to be added = "btechgeeks"

Output:

The given list after appending { btechgeeks } =  ['hello', 'this', 'is', 'btechgeeks']

Example2:

Input:

Given list = [10, 15, 20, 25]
Item to be added = 30

Output:

The given list after appending { 30 } = [10, 15, 20, 25, 30]

List append() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

1)Appending an item to List

Approach:

  • Give the list as static input and store it in a variable.
  • Give the string to be added as static input and store it in another variable.
  • Append the above-given string item to the given list using the append() function.
  • Print the given list after appending(adding) the given string item.
  • The Exit of Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ["hello", "this", "is"]
# Give the string to be added as static input and store it in another variable.
new_item = "btechgeeks"
# Append the above-given string item to the given list using the append()
# function.
gvn_lst.append(new_item)
# Print the given list after appending(adding) the given string item.
print("The given list after appending {", new_item, "} = ", gvn_lst)

Output:

The given list after appending { btechgeeks } =  ['hello', 'this', 'is', 'btechgeeks']
2)Appending List to a List

Approach:

  • Give the list as static input and store it in a variable.
  • Give the new list to be added as static input and store it in another variable.
  • Append the above-given new list to the given list using the append() function.
  • Print the given list after appending the given new list.
  • The Exit of Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ["good", "morning", "btechgeeks"]
# Give the new list to be added as static input and store it in another variable.
new_lst = [1, 2, 3, 4, 5]
# Append the above-given new list to the given list using the append()
# function.
gvn_lst.append(new_lst)
# Print the given list after appending the given new list.
print("The given list after appending new list ", new_lst, ":")
print(gvn_lst)

Output:

The given list after appending new list  [1, 2, 3, 4, 5] :
['good', 'morning', 'btechgeeks', [1, 2, 3, 4, 5]]
Method #2: Using Built-in Functions (User Input)
1)Appending an item to List

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the item to be added as user input using the int(input()) function and store it in another variable.
  • Append the above-given item to the given list using the append() function.
  • Print the given list after appending(adding) the given item.
  • The Exit of Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the item to be added as user input using the int(input()) function 
# and store it in another variable.
new_item = int(input("Enter some random number = "))
# Append the above-given item to the given list using the append()
# function.
gvn_lst.append(new_item)
# Print the given list after appending(adding) the given item.
print("The given list after appending {", new_item, "} = ", gvn_lst)

Output:

Enter some random List Elements separated by spaces = 10 15 20 25
Enter some random number = 30
The given list after appending { 30 } = [10, 15, 20, 25, 30]
2)Appending List to a List

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the new list to be added as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Append the above-given new list to the given list using the append() function.
  • Print the given list after appending the given new list.
  • The Exit of Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the new list to be added as user input using list(),map(),input(),and split() functions.
# Store it in another variable.
new_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Append the above-given new list to the given list using the append()
# function.
gvn_lst.append(new_lst)
# Print the given list after appending the given new list.
print("The given list after appending new list ", new_lst, ":")
print(gvn_lst)

Output:

Enter some random List Elements separated by spaces = 100 200 300
Enter some random List Elements separated by spaces = 400 500 600
The given list after appending new list [400, 500, 600] :
[100, 200, 300, [400, 500, 600]]