Intermediate Python

List Operations

Intermediate Python

Check out a free preview of the full Intermediate Python course

The "List Operations" Lesson is part of the full, Intermediate Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina reviews how to manipulate string values with sum(), min(), max(), and sorted().


Transcript from the "List Operations" Lesson

>> Nina Zakharenko: A few really useful operations in Python, going back to my simple list of squares, so that would be for num in range of six, and num times num So if we look at our squares, they're right here. Python has a sum operation that will let me quickly sum up the values of any numbers, as a sequence.

All I have to do is call the sum built in, and pass that list to it. So, some squares
>> Nina Zakharenko: There is also a built-in min which gives you the minimum value and built-in max. So biggest number, smallest number. We can also use some of the other built-in operations, we look at sorted and introduction to Python, so that we'll give us the sorted numbers that are already sorted in ascending order.

What if I wanted to get a copy of the sorted in Descending order.
>> Student1: Passing an argument.
>> Nina Zakharenko: Passing an argument. Does anyone remember what it is?
>> Student1: Sending.
>> Student2: Reverse equal.
>> Nina Zakharenko: Close. But yes, reverse=True. That'll give me back a list that's sorted in reverse. And just very quickly, one more list comprehension.

Let's say I had a.
>> Nina Zakharenko: Say I had a string here, Lottery_numbers_string, and it contained my Lottery numbers.Knowing everything we know now.How would we get.The. Maximum value.In this list of strings that are numbers.How would we get the maximum value. What's the first thing that we would need? What does max take?

>> Student3: OS.
>> Nina Zakharenko: OS. And what do the items in the list have to be?
>> Student3: String.
>> Student1: Numbers.
>> Student3: Verse.
>> Student1: Imaging
>> Nina Zakharenko: They have to be numbers. So, how do we get a list of numbers from this string?
>> Nina Zakharenko: How do we get a list of strings from this string?

>> Student1: Call split?
>> Nina Zakharenko: Split, and I'm just gonna make this a little shorter. I'm gonna reassign my variable, just so it's a little bit easier to read. We're now reassigning to lottery_nums. So I'm going to say lottery_nums.split(), what is my delimiter to split?
>> Student1: Comma.
>> Nina Zakharenko: Comma, and here I'm gonna have to add a space, right?

Okay, now I have a list of strings. We know that max takes a list of integers
>> Nina Zakharenko: How do I get my list of strings to be a list of integers?
>> Nina Zakharenko: Okay, [INAUDIBLE] comprehension, right? So we're gonna loop over for num in lottery_nums, but we're gonna call split right in here.

And we're gonna split on the comma space delimiter. What do we wanna take? What action do we wanna take on each string in this list?
>> Student2: Pass it to int.
>> Nina Zakharenko: Pass it to int, exactly right.
>> Nina Zakharenko: Okay, now we have a list of numbers and as we saw earlier, I mean this is getting a little bit complicated.

You'll probably want to introduce this as a variable, right. But as you're typing along in the you can build on this. So what does max take? A list of numbers, and now we have a list of numbers, right? So I can just put max, and open parenthesis, and put a closing parenthesis around all of that, I'll pass it to max.

And that gives me the biggest number in my list.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now