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

Python List Items Adding

In this Python tutorial, we will cover different ways to add items to a list. We will focus on the following methods:

  1. append()
  2. extend()
  3. insert()
  4. Using the + operator
  5. Using the * operator

1. append()

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]

2. 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]

3. 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]

4. Using the + operator

You 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]

5. Using the * operator

You 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.

  1. Appending elements to a list in Python:

    • Description: Appending involves adding a single element to the end of a list.
    • Example Code:
      my_list = [1, 2, 3]
      my_list.append(4)
      print(my_list)  # Output: [1, 2, 3, 4]
      
  2. Extend list in Python with new items:

    • Description: The extend() method is used to add multiple elements from an iterable to the end of a list.
    • Example Code:
      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]
      
  3. Inserting items into a specific position in a list in Python:

    • Description: The insert() method allows you to add an element at a specific position in the list.
    • Example Code:
      my_list = [1, 2, 3, 5]
      my_list.insert(3, 4)
      print(my_list)  # Output: [1, 2, 3, 4, 5]
      
  4. Concatenate lists in Python:

    • Description: Concatenation involves combining two or more lists into a single list.
    • Example Code:
      list1 = [1, 2, 3]
      list2 = [4, 5, 6]
      concatenated_list = list1 + list2
      print(concatenated_list)  # Output: [1, 2, 3, 4, 5, 6]
      
  5. Using the '+' operator to add elements to a list in Python:

    • Description: The + operator can be used for list concatenation, similar to the extend() method.
    • Example Code:
      my_list = [1, 2, 3]
      my_list = my_list + [4, 5, 6]
      print(my_list)  # Output: [1, 2, 3, 4, 5, 6]
      
  6. Appending multiple elements to a list in one go in Python:

    • Description: Appending multiple elements involves using the += operator or extend() method.
    • Example Code:
      my_list = [1, 2, 3]
      my_list += [4, 5, 6]
      print(my_list)  # Output: [1, 2, 3, 4, 5, 6]
      
  7. Adding elements to a list using list comprehension in Python:

    • Description: List comprehension is a concise way to add elements to a list based on a condition.
    • Example Code:
      my_list = [x for x in range(5)]
      print(my_list)  # Output: [0, 1, 2, 3, 4]
      
  8. Updating a list with new items in Python:

    • Description: Updating involves modifying existing elements or adding new elements to a list.
    • Example Code:
      my_list = [1, 2, 3, 4]
      my_list[2] = 5
      print(my_list)  # Output: [1, 2, 5, 4]