Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
In this Python tutorial, we will cover different ways to add items to a list. We will focus on the following methods:
append()
extend()
insert()
+
operator*
operatorappend()
The append()
method adds an item to the end of a list.
Example:
my_list = [1, 2, 3] my_list.append(4) print(my_list) # Output: [1, 2, 3, 4]
extend()
The extend()
method adds multiple items to a list by extending it with another iterable (e.g., list, tuple, string).
Example:
my_list = [1, 2, 3] my_list.extend([4, 5, 6]) print(my_list) # Output: [1, 2, 3, 4, 5, 6]
insert()
The insert()
method adds an item to a list at a specified index. The first argument is the index where you want to insert the item, and the second argument is the item itself.
Example:
my_list = [1, 2, 4] my_list.insert(2, 3) # Insert the number 3 at index 2 print(my_list) # Output: [1, 2, 3, 4]
+
operatorYou can use the +
operator to concatenate two lists, which creates a new list with the combined elements.
Example:
list1 = [1, 2, 3] list2 = [4, 5, 6] combined_list = list1 + list2 print(combined_list) # Output: [1, 2, 3, 4, 5, 6]
*
operatorYou can use the *
operator to create a new list by repeating the elements of an existing list a specified number of times.
Example:
my_list = [1, 2, 3] repeated_list = my_list * 3 print(repeated_list) # Output: [1, 2, 3, 1, 2, 3, 1, 2, 3]
In this tutorial, you learned different ways to add items to a Python list, including using the append()
, extend()
, and insert()
methods, as well as the +
and *
operators.
Appending elements to a list in Python:
my_list = [1, 2, 3] my_list.append(4) print(my_list) # Output: [1, 2, 3, 4]
Extend list in Python with new items:
extend()
method is used to add multiple elements from an iterable to the end of a list.my_list = [1, 2, 3] new_elements = [4, 5, 6] my_list.extend(new_elements) print(my_list) # Output: [1, 2, 3, 4, 5, 6]
Inserting items into a specific position in a list in Python:
insert()
method allows you to add an element at a specific position in the list.my_list = [1, 2, 3, 5] my_list.insert(3, 4) print(my_list) # Output: [1, 2, 3, 4, 5]
Concatenate lists in Python:
list1 = [1, 2, 3] list2 = [4, 5, 6] concatenated_list = list1 + list2 print(concatenated_list) # Output: [1, 2, 3, 4, 5, 6]
Using the '+' operator to add elements to a list in Python:
+
operator can be used for list concatenation, similar to the extend()
method.my_list = [1, 2, 3] my_list = my_list + [4, 5, 6] print(my_list) # Output: [1, 2, 3, 4, 5, 6]
Appending multiple elements to a list in one go in Python:
+=
operator or extend()
method.my_list = [1, 2, 3] my_list += [4, 5, 6] print(my_list) # Output: [1, 2, 3, 4, 5, 6]
Adding elements to a list using list comprehension in Python:
my_list = [x for x in range(5)] print(my_list) # Output: [0, 1, 2, 3, 4]
Updating a list with new items in Python:
my_list = [1, 2, 3, 4] my_list[2] = 5 print(my_list) # Output: [1, 2, 5, 4]