-1

I have a sets with student-name and marks inside list.

marks = [{"Rajesh", 76}, {"Suresh", 56}, {"Vijay", 62}]

I want to get greater than 60 marks from the above sets.

Result:

[{"Rajesh", 76},{"Vijay", 62}]

I have tried

marks = [{"Rajesh", 76}, {"Suresh", 56}, {"Vijay", 62}]
hi= [i for i in marks if list(i)[1]>60]
print(hi)
deceze
  • 510,633
  • 85
  • 743
  • 889
Rajesh
  • 31
  • 6
  • do you want to filter on list of sets? – Deepak Tripathi Oct 13 '22 at 06:19
  • 2
    Sets are not an appropriate data structure for storing the student name and mark data, can you change them to something else like a tuple or dict? – Iain Shelvington Oct 13 '22 at 06:19
  • 1
    Sets are unordered structures so you can't use index to access an element. I'd just convert it to a list of lists. `marks_lists = [list(i) for i in marks]` And then use the resulting list of lists. – NotAName Oct 13 '22 at 06:20
  • 1
    The order of values in sets is undefined. It's not guaranteed at all that `list(i)[1]` will refer to the mark. – deceze Oct 13 '22 at 06:20
  • Please read [ask] and **ask a question** when posting. Saying "I have tried " is not helpful, because it does not tell us: **what happened** when you tried running the code? **How is that different** from what you want? **What is your understanding** of that difference? **What do you need to know** in order to solve the problem? – Karl Knechtel Oct 13 '22 at 06:27

2 Answers2

1

Your issue lies in using sets to represent student data. The order of elements in a set is not guaranteed. You may wish to use a list or dictionary that you can predictably index into.

marks = [["Rajesh", 76], ["Suresh", 56], ["Vijay", 62]]

above60 = [x for x in marks 
             for _, m in (x,) 
             if m > 60]
Chris
  • 26,361
  • 5
  • 21
  • 42
1

you are using set data structures which is unordered. Change to tuple or list.

marks =[("Rajesh", 76), ("Suresh", 56), ("Vijay", 62)]
hi =[(i,j) for i,j in marks if j > 60]
print(hi)
Synonian_raj
  • 154
  • 1
  • 13