Python List

Python List Introduction:

Lists are one of the most commonly used data structures in Python. A list is a collection of elements, which can be of any type, including numbers, strings, and other lists etc. Lists are mutable, which means you can make changes in the list by adding, removing, or changing elements.

List declaration:

To declare a list in Python, you can use square brackets [] and separate the elements with commas. Here’s an example:

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

In the above example, we created a list called new_list that contains five integers.

You can also create an empty list by using the list() function or just an empty set of square brackets [].  Here are some examples:

list_1 = list()
list_2 = []

Both of the examples above create an empty list called empty_list_1 and list_2.

You can also create a list of a specific size filled with a default value using the * operator.  Here’s an example:

empty_list_1 = []
empty_list_2 = list()

In the example above, we created a list called my_list that contains three 1 values.

my_list = [1, 1, 1]

Python list features:

  1. Mutable: Lists are mutable, meaning you can modify their elements by assigning new values to specific indices.
  2. Ordered: Lists maintain the order of elements as they are added.
  3. Dynamic Size: Python lists can dynamically grow or shrink in size as elements are added or removed.
  4. Heterogeneous Elements: Lists can contain elements of different data types. For example, a single list can store integers, floats, strings, or even other lists.
  5. Indexing and Slicing: You can access individual elements in a list using square bracket notation and their index. Additionally, you can slice lists to extract a portion of elements by specifying start and end indices.
  6. Iteration: Lists can be easily iterated over using loops or list comprehensions, allowing you to process each element or perform operations on the entire list.
  7. Built-in Functions: Python provides a range of built-in functions specifically designed for working with lists. These include functions like `len()`, `max()`, `min()`, `sum()`, `sorted()`, and more.
  8. Versatile Data Structure: Lists are a versatile data structure used in a variety of scenarios.
  9. List Comprehensions: Python allows you to create new lists by performing operations on existing lists using concise and expressive syntax called list comprehensions.
  10. Extensive Methods: Python lists come with a range of built-in methods that enable various operations like adding or removing elements, sorting, reversing, searching, and more.

Python List Indexing and Slicing

In Python first element in the list has an index of 0. You can access elements in a list by their index using square brackets. Here’s an example:

# Define a list
my_list = ['apple', 'banana', 'cherry']

# Access the first element
first_element = my_list[0] # 'apple'

# Access the second element
second_element = my_list[1] # 'banana'

# Access the third element
third_element = my_list[2] # 'cherry'

# Print the elements
print("First element:", first_element)
print("Second element:", second_element)
print("Third element:", third_element)

You can also use negative indexing to access the list elements in the reverse order. Here’s an example:

# Define a list
my_list = ['apple', 'banana', 'cherry']

# Access the last element
last_element = my_list[-1] # 'cherry'

# Access the second-to-last element
second_last_element = my_list[-2] # 'banana'

# Access the third-to-last (or first) element
third_last_element = my_list[-3] # 'apple'

# Print the elements

print("Last element:", last_element)
print("Second-to-last element:", second_last_element)
print("Third-to-last element:", third_last_element)

Slicing rules to create a sublist:

Here are the rules for slicing in Python:

  1. Slicing uses the colon : operator to specify a range of indices. The syntax is my_list[start_index:end_index:step].
  2. The start_index is the index of the first element to include in the slice. If not specified, it defaults to 0.
  3. The end_index is the index of the first element to exclude from the slice. If not specified, it defaults to the length of the list.
  4. The step parameter specifies the step size between elements in the slice. If not specified, it defaults to 1.
  5. All parameters can be negative, in which case they specify the index relative to the end of the list. For example, my_list[-1] refers to the last element of the list.
  6. Slicing returns a new list that contains the specified range of elements from the original list.

You can also use slicing to access a subset of the list. Slicing allows you to extract a range of elements from the list. Here’s an example:

# Define a list
my_list = ['apple', 'banana', 'cherry', 'date', 'elderberry']

# Slice to get the first three elements
first_three = my_list[0:3] # ['apple', 'banana', 'cherry']

# Slice to get elements from index 2 to the end
from_second_onwards = my_list[2:] # ['cherry', 'date', 'elderberry']

# Slice to get the last two elements
last_two = my_list[-2:] # ['date', 'elderberry']

# Slice with a step of 2 (every second element)
every_second = my_list[::2] # ['apple', 'cherry', 'elderberry']

# Print the results
print("First three elements:", first_three)
print("From second onwards:", from_second_onwards)
print("Last two elements:", last_two)
print("Every second element:", every_second)

In the example above, we used slicing to extract a subset of the list that starts at index 0 and ends at index 3 (excluding index 3).


Updating list values:

Lists in Python are mutable, and their values can be updated by using the slice and assignment the ( = ) operator.

# Define a list
my_list = [1, 2, 3, 4, 5]

# Remove elements at indices 1 to 3
my_list[1:4] = []

# Print the updated list
print(my_list)

Iterating over a list

We can use a for loop to iterate over the list elements. Here’s an example:

# Define a list of fruits
fruits = ['apple', 'banana', 'cherry', 'date']

# Use a for loop to iterate over the list
for fruit in fruits:
print(fruit)

Membership operator in list

We can use operator (i.e. in or not in) on list elements. If an element is in list then it returns True. If an element is not in list it return False. Here’s an example:

# Example list
fruits = ['apple', 'banana', 'cherry', 'date']

# Using 'in' to check if an element is in the list
print('apple' in fruits) # Output: True
print('orange' in fruits) # Output: False


# Using 'not in' to check if an element is not in the list
print('grape' not in fruits) # Output: True
print('banana' not in fruits) # Output: False

Repetition on list

We can use the (*) operator for the repetition of a list. Here’s an example:

# Example list
fruits = ['apple', 'banana', 'cherry']

# Using * operator for repetition
repeated_list = fruits * 3

print(repeated_list)
# Output: ['apple', 'banana', 'cherry', 'apple', 'banana', 'cherry', 'apple', 'banana', 'cherry']

Concatenation of a list

We can use the (+) operator for the concatenation of a list. Here’s an example:

# Example lists
list1 = ['apple', 'banana']
list2 = ['cherry', 'date']

# Using + operator for concatenation
combined_list = list1 + list2

print(combined_list)
# Output: ['apple', 'banana', 'cherry', 'date']

Leave a Comment