How to use Python - Functions with Lists

Опубликовано: 21 Июнь 2020
на канале: Practical Python Solutions
437
7

We will continue our discussion on functions in this video by sending lists and dictionaries as arguments to our functions.

This is really not that different than what we've been doing but the fact that the lists and dictionaries are mutable objects in Python some interesting things can happen. So we just want to make sure that we understand these nuances.

I've created two lists here… one with a list of five user names and one empty list for validated users. The idea is that we are going to simulate an online system or a web server that takes in a list of names and validates them against some criteria before allowing them access to the site.

We're not actually going to validate the users but we'll simulate the validation process by copying the users after they have been validated to another list called validated users.

Our function will be called “Validate” and will accept two parameters…... one is the list of usernames requests and one empty list to store the newly validated users

The function takes both of these lists and we have a while loop that runs as long as there are items in the username list. For each item in the username list, the routine removes the last item with the pop statement. The pop statement also saves the item just removed into a variable called “vuser.” It keeps removing the last item in the list until we reach the end of the list.

Then, as part of our phony validation process, we print out that the username has been validated. Next, we add the user to the list of validated users list and that's basically our function

We call the function down here and we send the two lists mentioned earlier as arguments.

When we run this, we get our list of users, showing that they have been validated. These users now should be moved out of the usernames list and into validated users list.

We can show the results from the function here by printing out the validated users and printing the username list. The empty brackets here means that there are no entries remaining in the username list – they have all been copied to the validated user list.

A key point to note here is that my original list up here has now been changed. This list is empty and the original empty list that we sent to the function has been populated.
Insert -

As we saw in our illustration, When you send a list to a function in Python it only sends a reference to the list. A reference is like an alias or a pointer … it is not the list itself. Therefore, when we modify the list in our function we actually modify the contents in original list. We do not make a copy. That's a key point to understand.


There are ways of sending a copy of the list but I’m really not sure that that makes this less complicated. It's probably not the recommended way for beginners unless you really need to preserve the original list.

For now, it is important to be aware you are not sending the list to the function, you're sending a pointer to a list and any modifications that you do in your function will impact the original list.

The same rules for lists apply with dictionaries. We have our dictionary here - our spam, eggs and ham dictionary. We will print it before we send it to the function.

The function is written to remove SPAM from the list if it exists. This routine is executed inside the function.
After the function runs, we'll print the breakfast dictionary to see the difference to the original dictionary

So we run this and as you can see the function has all the items before it goes to the function. the function searchs the dictionary for spam and when it finds it it deletes it.
Now, if we print the dictionary after the function
we see that spam has been removed.

So it's the same for dictionaries and lists and the reason it's a little tricky with
both is because they're mutable.
Strings are also for example objects but because of strings are immutable they're unchangeable there's less likely to have undesired implications as a result of
Strings so the important thing to understand is the difference between passing a reference and a value to a function

I think that concludes our lesson on functions. We've covered the basics and you should be able to use them in your code with what you've learned here. The next lesson we're going to look at modules and and personally I think modules is where the main jump of programming comes because that'll allow you to use other people's code and at the end of the day that's what we're trying to do here to leverage other code to write games and business applications we can't do this all ourselves so modules is a key way to do it's the only way to do a lot of that so

if you want the code for this video go to my github page at the link below. I hope you have enjoyed this video and if you have please don’t forget to like, share and subscribe.
Thanks so much see you soon
use Python Functions


Смотрите видео How to use Python - Functions with Lists онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Practical Python Solutions 21 Июнь 2020, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 437 раз и оно понравилось 7 людям.