# Basic syntax:
first_list.append(second_list) # Append adds the second_list as an
# element to the first_list
first_list.extend(second_list) # Extend combines the elements of the
# first_list and the second_list
# Note, both append and extend modify the first_list in place
# Example usage for append:
first_list = [1, 2, 3, 4, 5]
second_list = [6, 7, 8, 9]
first_list.append(second_list)
print(first_list)
--> [1, 2, 3, 4, 5, [6, 7, 8, 9]]
# Example usage for extend:
first_list = [1, 2, 3, 4, 5]
second_list = [6, 7, 8, 9]
first_list.extend(second_list)
print(first_list)
--> [1, 2, 3, 4, 5, 6, 7, 8, 9]
#append to list
lst = [1, 2, 3]
li = 4
lst.append(li)
#lst is now [1, 2, 3, 4]
.append("the add"): append the object to the end of the list.
.insert("the add"): inserts the object before the given index.
.extend("the add"): extends the list by appending elements from the iterable.
# Add to List
my_list * 2 # [1, 2, '3', True, 1, 2, '3', True]
my_list + [100] # [1, 2, '3', True, 100] --> doesn't mutate original list, creates new one
my_list.append(100) # None --> Mutates original list to [1, 2, '3', True, 100] # Or: <list> += [<el>]
my_list.extend([100, 200]) # None --> Mutates original list to [1, 2, '3', True, 100, 200]
my_list.insert(2, '!!!') # None --> [1, 2, '!!!', '3', True] - Inserts item at index and moves the rest to the right.
' '.join(['Hello','There'])# 'Hello There' --> Joins elements using string as separator.
#.append() is a function that allows you to add values to a list
sampleList.append("Bob")
print ("Bob should appear in the list:", sampleList)
#The output will be:
Bob should appear in the list: ['Bob']
How to append element to a list in Python
# Programming list
programming_list = ['C','C#','Python','Java','JavaScript']
# append the HTML item to the list
programming_list.append('HTML')
print('The new list is :', programming_list)
How to append a list into an existing list
# Programming list
programming_list = ['C','C#','Python','Java']
frontend_programming =['CSS','HTML','JavaScript']
# append the frontend_progamming list into the existing list
programming_list.append(frontend_programming)
# Note that entire list is appended as a single element
print('The new appended list is :', programming_list)
fruits = ["Apple", "Banana"]
# 1. append()
print(f'Current Fruits List {fruits}')
f = input("Please enter a fruit name:
")
fruits.append(f)
print(f'Updated Fruits List {fruits}')