We provide effective and economically affordable training courses for R and Python, Click here for more details and course registration !

  1. Passing a list to function

When we perform data analysis using Python, it is often useful to pass a list in calling the function as we want each element of the list to be accessed by the function. Say the following code example shows a function hello_people() that is created to say hello to each person. By calling and passing a list of persons to this function, each person in the list is getting a hello welcome.

#create a function hello_people()
def hello_people(names):
    """Say hello to each person in the list."""
    for name in names:
        statement = "Hello, " + name.title() + "!"
        print(statement)

#create a list of people
persons = ['wei', 'mico', 'shirley']
#call function
hello_people(persons)

#output
Hello, Wei!
Hello, Mico!
Hello, Shirley!

2. Modify a list in a function

Python has a specific feature of permanently modifying the list that is passed to and modified inside the function.

First, let us see a task that moving each element from list-A to list-B without using function. Following code example shows by using a while loop, each task is implemented and moved from the uncompleted list to the completed list.

#a list for uncompleted tasks
uncompleted_tasks = ['vacuum', 'mop', 'dishwash']
#starting with an empty list for completed tasks
completed_tasks = []
# Using a while loop, to simulate implementing each task
# Then move each one from uncompleted list to completed list
while uncompleted_tasks:
    current_task = uncompleted_tasks.pop()
    # Simulate creating a 3D print from the design.
    print("Current task: " + current_task)
    completed_tasks.append(current_task)

# Display all completed tasks.
print("\nThe current task being done:")
for completed_task in completed_tasks:
    print(completed_task)

#output
Current task: dishwash
Current task: mop
Current task: vacuum

The current task being done:
dishwash
mop
vacuum

The code example could be more effective if we create two functions, one for moving tasks and the other for printing tasks.


#A function of implementing each task, and move each one 
#from uncompleted list to the completed list
def move_tasks(uncompleted_tasks, completed_tasks):
    """
    Simulate performing each task, until there are none left.
    Move each task to completed_tasks after finishing.
    """
    while uncompleted_tasks:
        current_task = uncompleted_tasks.pop()
    

        print("Current task: " + current_task)
        completed_tasks.append(current_task)
 
#A function for pringing each completed task       
def show_completed_tasks(completed_tasks):
    """Show all the models that were printed."""
    print("\nThe following tasks have been completed:")
    for completed_task in completed_tasks:
        print(completed_task)
        
#start with full uncompleted list and empty complted list        
uncompleted_tasks = ['vacuum', 'mop', 'dishwash']
completed_tasks = []

#call the functions
move_tasks(uncompleted_tasks, completed_tasks)
show_completed_tasks(completed_tasks)
#show uncompleted list
uncompleted_tasks  

#output
Current task: dishwash
Current task: mop
Current task: vacuum

The following tasks have been completed:
dishwash
mop
vacuum
Out[27]: []       #empty now

We seen that the list for uncompleted tasks is empty after we have called the functions, as the list has been passed to and modified inside the function.

The question now is how can we do if we do not want the list being passed to the function and not being modified. You can achieve this by passing a copy of the list to the function, so the original list will not be modified. It is shown in the following code example.

uncompleted_tasks = ['vacuum', 'mop', 'dishwash']
completed_tasks = []

#passing the copy by using [:] symbol, the original list
#will not be modified
move_tasks(uncompleted_tasks[:], completed_tasks)
show_completed_tasks(completed_tasks)
uncompleted_tasks

#output
Current task: dishwash
Current task: mop
Current task: vacuum

The following tasks have been completed:
dishwash
mop
vacuum
Out[28]: ['vacuum', 'mop', 'dishwash']    

You can also watch the full video on Python training from our YouTube channel.


0 Comments

Leave a Reply

Avatar placeholder