Newline (NuBe Question)

avi.e.gross at gmail.com avi.e.gross at gmail.com
Fri Nov 24 22:45:54 EST 2023


Grizz[l]y,

I think the point is not about a sorted list or sorting in general It is
about reasons why maintaining a data structure such as a list in a program
can be useful beyond printing things once. There are many possible examples
such as having a list of lists containing a record where the third item is a
GPA for the student and writing a little list comprehension that selects a
smaller list containing only students who are Magna Cum Laude or Summa Cum
Laude. 

studs = [
  ["Peter", 82, 3.53],
  ["Paul", 77, 2.83],
  ["Mary", 103, 3.82] 
]
  
magna = [stud for stud in studs if stud[2] >= 3.5 ]
summa = [stud for stud in studs if stud[2] >= 3.75 ]

print(studs, magna, summa, sep="\n")

OUTPUT:

>>> print(studs, magna, summa, sep="\n")
[['Peter', 82, 3.53], ['Paul', 77, 2.83], ['Mary', 103, 3.82]]
[['Peter', 82, 3.53], ['Mary', 103, 3.82]]
[['Mary', 103, 3.82]]

Of course, for serious work, some might suggest avoiding constructs like a
list of lists and switch to using modules and data structures that are often
more efficient to represent your data such as some form of matrix or
data.frame.

And, yes, you can sort something like the above by name or GPA or number of
credits taken but the point was responding to why bother making a list just
to print it. The answer is that many and even most programs do a bit more
than that and a good choice of data structure facilitates ...




-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com at python.org> On
Behalf Of Grizzy Adams via Python-list
Sent: Thursday, November 16, 2023 8:41 AM
To: python-list at python.org
Subject: Re: Newline (NuBe Question)

Thursday, November 16, 2023  at 7:47, Thomas Passin via Python-list wrote:
Re: Newline (NuBe Question) (at least in part)

>I wrote that you don't need the "students" list, which is correct.  But 
>there could be a use for a list.  It would let you change the order in 
>which students appear in the printed output.  Knowing how to do that is 
>a useful skill.  But that should be left for a later lesson, not mixed 
>in here.

I have a vague memory of seeing sorted list somewhere ;->)
-- 
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list