Skip to content

Latest commit

 

History

History
174 lines (126 loc) · 4.68 KB

File metadata and controls

174 lines (126 loc) · 4.68 KB

Python List Methods Illustrated

Slide 1: Introduction to Python List Methods

Python lists are versatile data structures that offer various built-in methods for manipulation. This slideshow will explore common list methods using food-themed examples.

# Creating a sample list
food_menu = ['🍔', '🍔', '🍟']
print(f"Initial menu: {food_menu}")

Slide 2: Append Method

The append() method adds an element to the end of a list.

food_menu = ['🍔', '🍔', '🍟']
food_menu.append('🍔')
print(f"Menu after append: {food_menu}")
# Output: Menu after append: ['🍔', '🍔', '🍟', '🍔']

Slide 3: Count Method

The count() method returns the number of occurrences of a specified element in the list.

food_menu = ['🍔', '🍔', '🍟']
burger_count = food_menu.count('🍔')
print(f"Number of burgers: {burger_count}")
# Output: Number of burgers: 2

Slide 4: Method

The () method creates a shallow of the list.

food_menu = ['🍔', '🍔', '🍟']
new_menu = food_menu.()
print(f"Copied menu: {new_menu}")
# Output: Copied menu: ['🍔', '🍔', '🍟']

Slide 5: Index Method

The index() method returns the index of the first occurrence of a specified element.

food_menu = ['🍔', '🍔', '🍟']
fries_index = food_menu.index('🍟')
print(f"Index of fries: {fries_index}")
# Output: Index of fries: 2

Slide 6: Reverse Method

The reverse() method reverses the order of elements in the list in-place.

food_menu = ['🍔', '🍔', '🍟']
food_menu.reverse()
print(f"Reversed menu: {food_menu}")
# Output: Reversed menu: ['🍟', '🍔', '🍔']

Slide 7: Remove Method

The remove() method removes the first occurrence of a specified element from the list.

food_menu = ['🍔', '🍔', '🍟']
food_menu.remove('🍟')
print(f"Menu after removing fries: {food_menu}")
# Output: Menu after removing fries: ['🍔', '🍔']

Slide 8: Insert Method

The insert() method adds an element at a specified position in the list.

food_menu = ['🍔', '🍔', '🍟']
food_menu.insert(1, '🥤')
print(f"Menu after inserting drink: {food_menu}")
# Output: Menu after inserting drink: ['🍔', '🥤', '🍔', '🍟']

Slide 9: Pop Method with Index

The pop() method removes and returns the element at a specified index. If no index is provided, it removes and returns the last element.

food_menu = ['🍔', '🍔', '🍟']
removed_item = food_menu.pop(1)
print(f"Removed item: {removed_item}")
print(f"Updated menu: {food_menu}")
# Output:
# Removed item: 🍔
# Updated menu: ['🍔', '🍟']

Slide 10: Pop Method without Index

When pop() is called without an argument, it removes and returns the last element.

food_menu = ['🍔', '🍔', '🍟']
last_item = food_menu.pop()
print(f"Last item removed: {last_item}")
print(f"Updated menu: {food_menu}")
# Output:
# Last item removed: 🍟
# Updated menu: ['🍔', '🍔']

Slide 11: Real-Life Example - Recipe Ingredients

Let's use list methods to manage a recipe's ingredients.

recipe = ['flour', 'sugar', 'eggs', 'milk']
print(f"Initial recipe: {recipe}")

# Add a new ingredient
recipe.append('vanilla')

# Remove an ingredient
recipe.remove('milk')

# Insert an ingredient at a specific position
recipe.insert(1, 'butter')

print(f"Updated recipe: {recipe}")
# Output:
# Initial recipe: ['flour', 'sugar', 'eggs', 'milk']
# Updated recipe: ['flour', 'butter', 'sugar', 'eggs', 'vanilla']

Slide 12: Real-Life Example - Task Management

Using list methods to manage a to-do list.

tasks = ['Read emails', 'Write report', 'Attend meeting']
print(f"Initial tasks: {tasks}")

# Add a new task
tasks.append('Review code')

# Mark the first task as complete (remove it)
completed_task = tasks.pop(0)

# Prioritize a task by moving it to the beginning
urgent_task = 'Call client'
tasks.insert(0, urgent_task)

print(f"Completed task: {completed_task}")
print(f"Updated tasks: {tasks}")
# Output:
# Initial tasks: ['Read emails', 'Write report', 'Attend meeting']
# Completed task: Read emails
# Updated tasks: ['Call client', 'Write report', 'Attend meeting', 'Review code']

Slide 13: Additional Resources

For more advanced list operations and in-depth explanations of Python data structures, refer to the following resources:

  1. Python official documentation on lists: https://docs.python.org/3/tutorial/datastructures.html
  2. "Mastering Python Lists" by Raymond Hettinger (PyCon 2017): https://www.youtube.com/watch?v=rhSKY4jzYIM

These resources provide comprehensive coverage of list methods and their efficient use in Python programming.