Practice problem doubt(12/1/23)

  1. Write a Python program to sort a list of tuples using Lambda.sir my doubt is why in this above program 'key"keyword used an dwhats the alternate solution for this program.?

subject_marks = [(‘English’, 88), (‘Science’, 90), (‘Maths’, 97), (‘Social sciences’, 82)]
print(“Original list of tuples:”)
print(subject_marks)
subject_marks.sort(key=lambda x: x[1])
print(“Sorting the List of Tuples:”)
print(subject_marks)

2)Write a Python program to find the values of length six in a given list using Lambda?

''p=(“bikash”,“rameshkumar”,“pragatkumar”,“ajay”)

m=filter(lambda i:“it is an six digit” if len(i)==6 else “not an six digit”,p)
for k in m:
print(k)
[please explain where my approach is wrong and if wrong solve it useing my approach]

3)Write a Python program to find whether a given string starts with a given character using Lambda.

2 Likes

Hiiii Bikash Pradhan,
Key is not keyword, It is a parameter in sort function, which tells you the value by which you what to sort the elements in list.
In this question you wanted to sort the values on the basis of 2nd element of each tuple that’s why you have written sort(key = lambda x: x[1])

1 Like

alternate solution for solving question 1 is you can use nested for loop, to do the sorting. Try it once if you face any problem then you can ask again

1 Like

@mybusiness6382
For question 2:
p=[‘bikash’,‘rameshkumar’,‘pragatkumar’,‘ajay’]

m=filter(lambda i: i if len(i)==6 else ‘’,p)
for k in m:
print(k)

Question 3 - solution

starts_with = lambda x: True if x.startswith(‘P’) else False
print(starts_with(‘Python’))
starts_with = lambda x: True if x.startswith(‘P’) else False
print(starts_with(‘Java’))

1 Like