xxxxxxxxxx
l1 = [1, 8, 7, 2, 21, 15]
l1.append(45) # adds 45 at the end of the list.
#it will append number at the end of list which number you will write.
xxxxxxxxxx
#a list
cars = ['Ford', 'Volvo', 'BMW', 'Tesla']
#append item to list
cars.append('Audi')
print(cars)
['Ford', 'Volvo', 'BMW', 'Tesla', 'Audi']
list = ['Hello', 1, '@']
list.append(2)
list
['Hello', 1, '@', 2]
list = ['Hello', 1, '@', 2]
list.append((3, 4))
list
['Hello', 1, '@', 2, (3, 4)]
list.append([3, 4])
list
['Hello', 1, '@', 2, (3, 4), [3, 4]]
list.append(3, 4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: append() takes exactly one argument (2 given)
list.extend([5, 6])
list
['Hello', 1, '@', 2, (3, 4), [3, 4], 5, 6]
list.extend((5, 6))
list
['Hello', 1, '@', 2, (3, 4), [3, 4], 5, 6, 5, 6]
list.extend(5, 6)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: extend() takes exactly one argument (2 given)
xxxxxxxxxx
list_1 = ['w','h']
list_1.append('y') # you need no veribal to store list_1.append('y')
print(list_1) # ['w','h','y']
list_2 = ['a','r','e']
list_1.append(list_2) # This also donot need a veribal to store it
print(list_1) # ['w','h','y',['a','r','e']]
list_1.extend(list_2)
print(list_1) # ['w','h','y',['a','r','e'],'a','r','e']
# please like
xxxxxxxxxx
my_list = ['a', 'b', 'c']
my_list.append('e')
print(my_list)
# Output
#['a', 'b', 'c', 'e']
xxxxxxxxxx
currencies = ['Dollar', 'Euro', 'Pound']
# append 'Yen' to the list
currencies.append('Yen')
print(currencies)
# Output: ['Dollar', 'Euro', 'Pound', 'Yen']
xxxxxxxxxx
# 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.
xxxxxxxxxx
#.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']
xxxxxxxxxx
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)