# sort() method = used with lists # sort() function = used with iterables students = (("Squidward", "F", 60), ("Sandy", "A", 33), ("Patrick","D", 36), ("Spongebob","B", 20), ("Mr.Krabs","C", 78)) grade = lambda grades:grades[1] # students.sort(key=age) # sorts current list sorted_students = sorted(students,key=grade) # sorts and creates a new list for i in sorted_students: print(i)
Honestly my brain went into the stupid mode when I was trying to digest the lambda function used to obtain the key by which the dataframe was going to be sorted Honestly I am not up to speed with lambda, and found the function defined in the old way more readable and easier to understand, as per my workings below # define a function to return a Series of items in a column (grades here), by which the Dataframe will be sorted def grab_item(items): return items[1] # the index number for grade items is 1 # now sort the DataFrame using the key 'grab_item' as established above students.sort(key=grab_item) # execute below for loop to print out the list of tuples, sorted by the grades column for i in students: print(i) Output per below ('Sandy', 'A', 33) ('Spongebob', 'B', 20) ('Mr.Krabs', 'C', 78) ('Patrick', 'D', 36) ('Squidward', 'F', 60)
i posted this on another comment, but i'll reply here too just in case you still wonder & aren't notified on the other thread; "grade = lambda grades:grades[1]" just marks the index to look at when passing this as a key in the sorted() function later on. grade = lambda grades:grades[1] # "grades" is just an argument you can name anything you want # in this case the way it's used later will actually refer to the student tuple & all 3 keys # "grades[1]" just refers to the index within the argument passed as "grades", 1 being grade, 2 would be the age. sorted_students = sorted(students,key=grade) # sorted takes 2 args, 1st: the list/tuple, 2nd: a key to sort by, in this case "key=grades", which just points to an index # written in regular function style below, replacing "grades" with "student" for clarity: students = (("Squidward", "F", 60), ("Sandy", "A", 33), ("Patrick","D", 36), ("Spongebob","B", 20), ("Mr.Krabs","C", 78)) def get_grades(student): return student[1] grade = get_grades sorted_students = sorted(students,key=grade) for i in sorted_students: print(i)
So.... Functions and methods are different? And, in the lambda function in 6:13 what exactly is ages and ages[2]? I mean, there is no defined list called ages... Is that list of tuple passed there with alias ages? And, can we create the key function normally? Like without the lambda operator? Thanks! PS: sorry for number and length of questions
The use of lambda creates an anonymous function (which is callable). In the case of sorted the callable only takes one parameters. Python's lambda is pretty simple. It can only do and return one thing really.
"grade = lambda grades:grades[1]" just marks the index to look at when passing this as a key in the sorted() function later on. grade = lambda grades:grades[1] # "grades" is just an argument you can name anything you want # in this case the way it's used later will actually refer to the student tuple & all 3 keys # "grades[1]" just refers to the index within the argument passed as "grades", 1 being grade, 2 would be the age. sorted_students = sorted(students,key=grade) # sorted takes 2 args, 1st: the list, 2nd: a key to sort by, in this case "key=grade", which just points to an index # written in regular function style below: students = (("Squidward", "F", 60), ("Sandy", "A", 33), ("Patrick","D", 36), ("Spongebob","B", 20), ("Mr.Krabs","C", 78)) def get_grades(student): grades = student[1] return grades grade = get_grades sorted_students = sorted(students,key=grade) for i in sorted_students: print(i)
@@Craulback so the labda fuction just returns an index of 1 and it doesnt hav anything to do with the student list right? cause we arnt passing in students as an argument. but the the index that it passes is used when sorting the list. correct me if im wrong pls
@@partlycloudy2346 the lambda function returns whatever is actually **in** index 1, but in the function itself no list is passed. you can then do what you want with that, like pass any list you want to sort by that index position through the built in sorted function, using the "grade" variable as the key "sorted_students = sorted(students,key=grade)" if you want to see what's happening you can print it: for student in students: print(grade(student))
Bro I literally watched it like +10 times and my brain still not able to compute this shit. HOW IS AGE FUNCTION USED WITHOUT ANY ARGUMENTS AND WHAT RELATE THE AGES[2] VARIABLE WITH THE LIST I DONT GET IT!!
@@MoreBilaINoFilter now I got it, age is just a variable wich contains a lambda but it doesn't do anything by itself, he just use thtat for the pick of each second parameter of each tuple in the dictionaire
# sort() method = used with lists
# sort() function = used with iterables
students = (("Squidward", "F", 60),
("Sandy", "A", 33),
("Patrick","D", 36),
("Spongebob","B", 20),
("Mr.Krabs","C", 78))
grade = lambda grades:grades[1]
# students.sort(key=age) # sorts current list
sorted_students = sorted(students,key=grade) # sorts and creates a new list
for i in sorted_students:
print(i)
He breaks down these complex topics into 3 minute videos!!
Truly Amazing
Loving your videos, short but great explanation! thx for sharing your knowledge 🎉😊
bro thank you i always learn things faster if i watch u
Honestly my brain went into the stupid mode when I was trying to digest the lambda function used to obtain the key by which the dataframe was going to be sorted
Honestly I am not up to speed with lambda, and found the function defined in the old way more readable and easier to understand, as per my workings below
# define a function to return a Series of items in a column (grades here), by which the Dataframe will be sorted
def grab_item(items):
return items[1] # the index number for grade items is 1
# now sort the DataFrame using the key 'grab_item' as established above
students.sort(key=grab_item)
# execute below for loop to print out the list of tuples, sorted by the grades column
for i in students:
print(i)
Output per below
('Sandy', 'A', 33)
('Spongebob', 'B', 20)
('Mr.Krabs', 'C', 78)
('Patrick', 'D', 36)
('Squidward', 'F', 60)
Try this it is the same: mylist = [(3, 5, 8), (6, 2, 8), ( 2, 9, 4), (6, 8, 5)]
print(sorted(mylist, key=lambda x: x[1]))
cars = [('citroen', 'xsara', 1100), ('lincoln', 'navigator', 2000), ('bmw', 'x5', 1700)]
print(sorted(cars, key=lambda car: car[0]))
print(sorted(cars, key=lambda car: car[1]))
print(sorted(cars, key=lambda car: car[2]))
i posted this on another comment, but i'll reply here too just in case you still wonder & aren't notified on the other thread;
"grade = lambda grades:grades[1]" just marks the index to look at when passing this as a key in the sorted() function later on.
grade = lambda grades:grades[1] # "grades" is just an argument you can name anything you want
# in this case the way it's used later will actually refer to the student tuple & all 3 keys
# "grades[1]" just refers to the index within the argument passed as "grades", 1 being grade, 2 would be the age.
sorted_students = sorted(students,key=grade) # sorted takes 2 args, 1st: the list/tuple, 2nd: a key to sort by, in this case "key=grades", which just points to an index
# written in regular function style below, replacing "grades" with "student" for clarity:
students = (("Squidward", "F", 60),
("Sandy", "A", 33),
("Patrick","D", 36),
("Spongebob","B", 20),
("Mr.Krabs","C", 78))
def get_grades(student):
return student[1]
grade = get_grades
sorted_students = sorted(students,key=grade)
for i in sorted_students:
print(i)
can you pls explain how the function works as well
wow you explained it so simply!
Keep this good work!!
Great explanations 😇
thank you
I like the way I learned from you bro 😅❣️✌👌
So.... Functions and methods are different?
And, in the lambda function in 6:13 what exactly is ages and ages[2]? I mean, there is no defined list called ages...
Is that list of tuple passed there with alias ages?
And, can we create the key function normally? Like without the lambda operator?
Thanks!
PS: sorry for number and length of questions
You are not alone on this one, please see my comments above
The use of lambda creates an anonymous function (which is callable). In the case of sorted the callable only takes one parameters. Python's lambda is pretty simple. It can only do and return one thing really.
"grade = lambda grades:grades[1]" just marks the index to look at when passing this as a key in the sorted() function later on.
grade = lambda grades:grades[1] # "grades" is just an argument you can name anything you want
# in this case the way it's used later will actually refer to the student tuple & all 3 keys
# "grades[1]" just refers to the index within the argument passed as "grades", 1 being grade, 2 would be the age.
sorted_students = sorted(students,key=grade) # sorted takes 2 args, 1st: the list, 2nd: a key to sort by, in this case "key=grade", which just points to an index
# written in regular function style below:
students = (("Squidward", "F", 60),
("Sandy", "A", 33),
("Patrick","D", 36),
("Spongebob","B", 20),
("Mr.Krabs","C", 78))
def get_grades(student):
grades = student[1]
return grades
grade = get_grades
sorted_students = sorted(students,key=grade)
for i in sorted_students:
print(i)
@@Craulback so the labda fuction just returns an index of 1 and it doesnt hav anything to do with the student list right? cause we arnt passing in students as an argument. but the the index that it passes is used when sorting the list. correct me if im wrong pls
@@partlycloudy2346 the lambda function returns whatever is actually **in** index 1, but in the function itself no list is passed.
you can then do what you want with that, like pass any list you want to sort by that index position through the built in sorted function, using the "grade" variable as the key "sorted_students = sorted(students,key=grade)"
if you want to see what's happening you can print it:
for student in students:
print(grade(student))
Bruh thank you so much, i was thinking about this literally overnight, and suddenly found this video.
Example is easy to understand
his only video ive ever thought he didnt explain well, guys, his human, get him lol
This video is so great!
Thanks bro!!!
Thanks
thanks
nice video
always surprising me!
thank you good sir
quite good
thank you sir
Thank you
جزاك الله خيرا
Thanks Bro!
ty bro
Thank you!
Thank you Bro!
Thanks bro
thx 4 vid bro !
Nice video
Thanks Aman!
Wow!
keep going Bro .....
great job !
thx Bro !!
I did it but i dont understand very well of lambda function
meow~!
Here is my random comment for your BRO ❤
can anyone please explain what value does grades hold in grades[1]
why the lambda is refering to the list of tuples? I don't get that
Bro I literally watched it like +10 times and my brain still not able to compute this shit. HOW IS AGE FUNCTION USED WITHOUT ANY ARGUMENTS AND WHAT RELATE THE AGES[2] VARIABLE WITH THE LIST I DONT GET IT!!
@@MoreBilaINoFilter now I got it, age is just a variable wich contains a lambda but it doesn't do anything by itself, he just use thtat for the pick of each second parameter of each tuple in the dictionaire
here's a comment for the algorithm bro ily
ily bro
comment
You are my life saver 💕💕💕 thanks a lot bro🫴🫴💕💕
thanks bro
thanks