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

In this Python List tutorial, we'll cover the basics of Python lists, including how to create, access, modify, and remove elements from a list. We'll also discuss some built-in list methods and common operations.

  1. Creating a list
  2. Accessing list elements
  3. Modifying list elements
  4. Removing list elements
  5. List slicing
  6. Built-in list methods

1. Creating a list

A list is an ordered collection of elements, which can be of any data type. Lists are mutable, which means you can modify their contents. To create a list, use square brackets [] and separate elements with commas.

my_list = [1, 2, 3, "apple", 4.5, True]

2. Accessing list elements

List elements can be accessed using indices, which start at 0 for the first element, 1 for the second element, and so on. Negative indices can be used to access elements from the end of the list.

my_list = [1, 2, 3, 4, 5]

print(my_list[0])  # Output: 1
print(my_list[2])  # Output: 3
print(my_list[-1])  # Output: 5

3. Modifying list elements

List elements can be modified using their indices:

my_list = [1, 2, 3, 4, 5]

my_list[1] = 20
print(my_list)  # Output: [1, 20, 3, 4, 5]

4. Removing list elements

You can remove list elements using the remove(), pop(), or del methods:

my_list = [1, 2, 3, 4, 5]

# Using remove()
my_list.remove(3)
print(my_list)  # Output: [1, 2, 4, 5]

# Using pop()
my_list.pop(1)
print(my_list)  # Output: [1, 4, 5]

# Using del
del my_list[0]
print(my_list)  # Output: [4, 5]

5. List slicing

List slicing allows you to create a new list from a portion of an existing list:

my_list = [1, 2, 3, 4, 5]

sliced_list = my_list[1:4]
print(sliced_list)  # Output: [2, 3, 4]

6. Built-in list methods

Python has several built-in list methods, such as:

  • append(): Adds an element to the end of the list.
  • extend(): Adds multiple elements to the end of the list.
  • insert(): Inserts an element at the specified index.
  • count(): Returns the number of occurrences of the specified element.
  • index(): Returns the index of the first occurrence of the specified element.
  • sort(): Sorts the list in ascending order (or using a custom comparison function).
  • reverse(): Reverses the order of the list.
  1. How to create a list in Python:

    • Description: Creating a list is simple and involves enclosing elements within square brackets.
    • Example Code:
      my_list = [1, 2, 3, 4, 5]
      
  2. Python list methods and functions:

    • Description: Lists in Python have various built-in methods and functions for manipulation.
    • Example Code:
      my_list = [6, 7, 8, 9, 10]
      length = len(my_list)
      maximum_value = max(my_list)
      my_list.append(11)
      
  3. Accessing elements in a Python list:

    • Description: Accessing elements involves using indices, where indexing starts from 0.
    • Example Code:
      my_list = ['apple', 'orange', 'banana']
      first_element = my_list[0]
      third_element = my_list[2]
      
  4. Adding elements to a Python list:

    • Description: Adding elements can be done using methods like append, insert, or the + operator.
    • Example Code:
      my_list = [11, 12, 13]
      my_list.append(14)
      my_list.insert(1, 15)
      new_elements = [16, 17]
      my_list += new_elements
      
  5. Removing items from a Python list:

    • Description: Removing items can be done using methods like remove, pop, or through slicing.
    • Example Code:
      my_list = [18, 19, 20, 19]
      my_list.remove(19)
      popped_item = my_list.pop(1)
      del my_list[0]
      
  6. Iterating through a Python list:

    • Description: Iterating through a list can be achieved using a for loop or list comprehension.
    • Example Code:
      my_list = ['cat', 'dog', 'rabbit']
      for animal in my_list:
          print(animal)
      
  7. Common operations with Python lists:

    • Description: Common operations include slicing, reversing, sorting, and checking membership.
    • Example Code:
      my_list = [21, 22, 23, 24, 25]
      sublist = my_list[1:4]
      reversed_list = list(reversed(my_list))
      sorted_list = sorted(my_list)
      is_present = 23 in my_list
      
  8. List comprehension in Python:

    • Description: List comprehension is a concise way to create lists using a single line of code.
    • Example Code:
      my_list = [26, 27, 28, 29, 30]
      squared_values = [x**2 for x in my_list]