Python- Day 25- FUNCTIONS- Passing a List
Lists are commonly passed to functions, such as lists of names, numbers, or complex objects like dictionaries.
When a list is passed to a function, the function gets direct access to its contents, allowing efficient handling of large data sets.
def greet_users(names):
for name in names:
msg = f"Hello, {name.title()}!"
print(msg)
usernames = ['Nidhi', 'Nid', 'Nidh']
greet_users(usernames)
>>
Hello, Nidhi!
Hello, Nid!
Hello, Nidh!
Function Access to List: The greet_users()
function directly accesses and processes the list of usernames.
Personalized Output: The function loops through the list and prints a personalized greeting for each user.
You can call the function anytime with a different list of users.
Modifying a List in a Function:
Modifications in Function: Functions can modify the original list. Changes made to the list inside the function are permanent.
Efficiency in Large Data Sets: By modifying the list directly, it is more efficient, especially when dealing with large data sets.
Example: Simulating 3D Model Printing
unprinted_designs = ['phone case', 'robot pendant', 'car']
completed_models = []
while unprinted_designs:
current_design = unprinted_designs.pop()
print(f"Printing model: {current_design}")
completed_models.append(current_design)
print("\nThe following models have been printed:")
for completed_model in completed_models:
print(completed_model)
>>
Printing model: car
Printing model: robot pendant
Printing model: phone case
The following models have been printed:
car
robot pendant
phone case
Moves unprinted designs to a list of completed models by simulating printing.
Permanently modifies both lists in the process.
Using Functions to Organize Code:
Refactoring the Model Printing Example:
The same program can be split into two functions for better organization and reusability.
def print_models(unprinted_designs, completed_models):
#Simulate printing each design.
while unprinted_designs:
current_design = unprinted_designs.pop()
print(f"Printing model: {current_design}")
completed_models.append(current_design)
def show_completed_models(completed_models):
#Show all the models that were printed.
print("\nThe following models have been printed:")
for completed_model in completed_models:
print(completed_model)
unprinted_designs = ['phone case', 'robot pendant', 'car']
completed_models = []
print_models(unprinted_designs, completed_models)
show_completed_models(completed_models)
>>
Printing model: car
Printing model: robot pendant
Printing model: phone case
The following models have been printed:
car
robot pendant
phone case
Separation of Concerns: Each function handles a single task.
print_models()
handles the simulation of printing.show_completed_models()
displays the completed models.
Modular Code: Makes the program easier to extend or modify.
- Example: If more models need to be printed later, simply call
print_models()
again.
Preventing a Function from Modifying a List
Modifying a list inside a function might remove the original list’s contents, which might need to be preserved.
Solution: Pass a copy of the list instead of the original.
print_models(unprinted_designs[:], completed_models)
Slice Notation [:]
: Creates a copy of the unprinted_designs
list.
The function modifies the copy, leaving the original list unchanged.
When to Pass a Copy:
- Use Case: Pass a copy when you want to preserve the original list.
- Default Behavior: By default, pass the original list unless there’s a specific need for a copy. Passing the original list is more efficient, especially for large lists, as it avoids using extra memory and processing time.
Passing Lists to Functions: Allows direct access and efficient manipulation of list contents.
Modifying Lists: Functions can permanently change a list, which is useful when processing large data sets.
Function Organization: Breaking down complex tasks into smaller functions makes code easier to understand, maintain, and extend.
Preserving Original Lists: Use list copies to prevent unintended modifications to the original list, although passing the original list is generally more efficient.
If you found this guide helpful then do click on 👏 the button.
Follow for more Learning like this 😊
If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!