Python Coding Day 9 | Python List, List Indexing, Changing Data, append(), extend()

Python List, List Indexing, Changing Data, append(), extend()

Watch the lesson tutorial  ðŸ”»


Python List: The Versatile Data Structure

A Python List is a fundamental data structure used to store collections of items.

  • Ordered: Items maintain a specific sequence.

  • Changeable (Mutable): You can modify the list after it's created.

  • Allows Duplicates: The list can contain identical values.

Practical Example Code

We'll start with a list of names:

Python
# Our list of names
names = ["Alice", "Bob", "Charlie", "David", "Eve", "Frank"]
print("The list of names is:", names)
print("The type of the variable is:", type(names))

🔢 List Indexing: Finding Items by Position

Every item in a Python list is assigned a position number called an index.

1. Positive Indexing (0, 1, 2, ...)

Python lists are zero-indexed, meaning the count starts at 0 for the first item.

Index012345
ValueAliceBobCharlieDavidEveFrank

Concept: You access items by referencing the index from the left side of the list.

2. Negative/Inverse Indexing (-1, -2, -3, ...)

You can also index from the end of the list. Negative indexing starts at -1 for the last item.

Index-6-5-4-3-2-1
ValueAliceBobCharlieDavidEveFrank

Concept: You access items by referencing the index from the right side of the list.

Practical Example Code for Indexing

Python
names = ["Alice", "Bob", "Charlie", "David", "Eve", "Frank"]

# Accessing with Positive Indexing
# Access the item at index 0 (the first item)
print("Item at index 0 (the first item):", names[0])  # Output: Alice
# Access the item at index 3
print("Item at index 3:", names[3])                  # Output: David

# Accessing with Negative Indexing
# Access the item at index -1 (the last item)
print("Item at index -1 (the last item):", names[-1]) # Output: Frank
# Access the item at index -4
print("Item at index -4:", names[-4])                 # Output: Charlie

🛠️ List Editing: Modifying the List

Since lists are changeable, here are the common methods you use to modify their contents.

1. Changing Data (Direct Assignment)

You can replace an existing item by using its index and assigning a new value to that position.

Python
student_names = ["John", "Michael", "Sarah", "Emily", "James", "Laura", "Robert"]
print("Original list:", student_names)

# Change the item at index 1 ("Michael") to "Zachary"
student_names[1] = "Zachary"

print("List after changing data:", student_names)
# Output: ['John', 'Zachary', 'Sarah', 'Emily', 'James', 'Laura', 'Robert']

2. append() (Add One Item)

The .append() method adds a single item to the end of the list.

Python
student_names = ["John", "Michael", "Sarah", "Emily", "James", "Laura", "Robert"]

# Append a new student name to the end
student_names.append("NewStudentCodes")

print("List after using append():", student_names)
# Output: ['John', 'Michael', 'Sarah', 'Emily', 'James', 'Laura', 'Robert', 'NewStudentCodes']

Remember: Always use parentheses () when calling list methods like append().

3. extend() (Add Multiple Items)

The .extend() method adds multiple items from another sequence (like a list or tuple) to the end of the current list.

Python
student_names = ["John", "Michael", "Sarah", "Emily", "James", "Laura", "Robert"]

# Extend the list by adding the elements of the new list ["Hello", "World"]
student_names.extend(["Hello", "World"])

print("List after using extend():", student_names)
# Output: ['John', 'Michael', 'Sarah', 'Emily', 'James', 'Laura', 'Robert', 'Hello', 'World']

Remember: .extend() expects a sequence of items (like a list) inside the parentheses.


📚 Summary of List Editing Methods

MethodPurposeCorrect Syntax Example
Direct AssignmentChange a single existing item.my_list[1] = "NewValue"
.append()Add a single item to the end.my_list.append("ItemX")
.extend()Add multiple items to the end.my_list.extend(["A", "B"])

- by Chirana Nimnaka

Comments

Popular posts from this blog

Python Coding Day 1 | The print() Function and Comments

What is Python?

Set Up an Environment to Code in Python