Files & List Comprehensions

In the last few lectures, we learned about sequences (strings, lists, ranges) and how to iterate over them using loops. Today we will look at how to read from files, and store the contents as a string or list of strings.

We will also look at some common operations involving lists, strings and counters that are useful when analyzing data.

(Ignore the following cell. It’s required for formatting this page.)

%%html
<style>
  table {margin-left: 0 !important;}
</style>

Reading from a File

We can easily read and write to files using Python.

To open a file for reading or writing, we use the built-in function open().

# 'r' means open the file for reading
book = open('textfiles/mountains.txt', 'r') 

Mode. The mode 'r' for reading the file is the default (and optional). So when reading from a file (instead of writing), we can just write:

book = open('textfiles/mountains.txt') 

With…as block and iterating over files

With block to open and close files. Whenever you open a file, you must also close it to prevent future problems like memory leaks. To avoid writing code to explicitly open and close, we will use the with...as block which keeps the file open within it, and automatically closes the file after exiting the block.

Within a with...as block, we can iterate over the lines of a file using a for loop in the same way as we would iterate over any sequence.

# read input file and print each line
with open('textfiles/mountains.txt') as book:
    for line in book:
        # print(line)
        print(line.strip())
# file is implicitly closed here
O, proudly rise the monarchs of our mountain land,
With their kingly forest robes, to the sky,
Where Alma Mater dwelleth with her chosen band,
And the peaceful river floweth gently by.

The mountains! The mountains! We greet them with a song,
Whose echoes rebounding their woodland heights along,
Shall mingle with anthems that winds and fountains sing,
Till hill and valley gaily gaily ring.

Beneath their peaceful shadows may old Williams stand,
Till the suns and mountains never more shall be,
The glory and the honor of our mountain land,
And the dwelling of the gallant and the free.

The mountains! The mountains! We greet them with a song,
Whose echoes rebounding their woodland heights along,
Shall mingle with anthems that winds and fountains sing,
Till hill and valley gaily gaily ring.
# read input file and use .split() to create a 
# list of strings *for each line*
with open('textfiles/mountains.txt') as book: 
    for line in book:
        print(line.strip().split()) 
['O,', 'proudly', 'rise', 'the', 'monarchs', 'of', 'our', 'mountain', 'land,']
['With', 'their', 'kingly', 'forest', 'robes,', 'to', 'the', 'sky,']
['Where', 'Alma', 'Mater', 'dwelleth', 'with', 'her', 'chosen', 'band,']
['And', 'the', 'peaceful', 'river', 'floweth', 'gently', 'by.']
[]
['The', 'mountains!', 'The', 'mountains!', 'We', 'greet', 'them', 'with', 'a', 'song,']
['Whose', 'echoes', 'rebounding', 'their', 'woodland', 'heights', 'along,']
['Shall', 'mingle', 'with', 'anthems', 'that', 'winds', 'and', 'fountains', 'sing,']
['Till', 'hill', 'and', 'valley', 'gaily', 'gaily', 'ring.']
[]
['Beneath', 'their', 'peaceful', 'shadows', 'may', 'old', 'Williams', 'stand,']
['Till', 'the', 'suns', 'and', 'mountains', 'never', 'more', 'shall', 'be,']
['The', 'glory', 'and', 'the', 'honor', 'of', 'our', 'mountain', 'land,']
['And', 'the', 'dwelling', 'of', 'the', 'gallant', 'and', 'the', 'free.']
[]
['The', 'mountains!', 'The', 'mountains!', 'We', 'greet', 'them', 'with', 'a', 'song,']
['Whose', 'echoes', 'rebounding', 'their', 'woodland', 'heights', 'along,']
['Shall', 'mingle', 'with', 'anthems', 'that', 'winds', 'and', 'fountains', 'sing,']
['Till', 'hill', 'and', 'valley', 'gaily', 'gaily', 'ring.']
# if we want to create one big list of the words, we can accumulate
# in a list using the extend() method
wordList = [] 
with open('textfiles/mountains.txt') as book:  
    for line in book:
        wordList.extend(line.strip().split())
wordList
['O,',
 'proudly',
 'rise',
 'the',
 'monarchs',
 'of',
 'our',
 'mountain',
 'land,',
 'With',
 'their',
 'kingly',
 'forest',
 'robes,',
 'to',
 'the',
 'sky,',
 'Where',
 'Alma',
 'Mater',
 'dwelleth',
 'with',
 'her',
 'chosen',
 'band,',
 'And',
 'the',
 'peaceful',
 'river',
 'floweth',
 'gently',
 'by.',
 'The',
 'mountains!',
 'The',
 'mountains!',
 'We',
 'greet',
 'them',
 'with',
 'a',
 'song,',
 'Whose',
 'echoes',
 'rebounding',
 'their',
 'woodland',
 'heights',
 'along,',
 'Shall',
 'mingle',
 'with',
 'anthems',
 'that',
 'winds',
 'and',
 'fountains',
 'sing,',
 'Till',
 'hill',
 'and',
 'valley',
 'gaily',
 'gaily',
 'ring.',
 'Beneath',
 'their',
 'peaceful',
 'shadows',
 'may',
 'old',
 'Williams',
 'stand,',
 'Till',
 'the',
 'suns',
 'and',
 'mountains',
 'never',
 'more',
 'shall',
 'be,',
 'The',
 'glory',
 'and',
 'the',
 'honor',
 'of',
 'our',
 'mountain',
 'land,',
 'And',
 'the',
 'dwelling',
 'of',
 'the',
 'gallant',
 'and',
 'the',
 'free.',
 'The',
 'mountains!',
 'The',
 'mountains!',
 'We',
 'greet',
 'them',
 'with',
 'a',
 'song,',
 'Whose',
 'echoes',
 'rebounding',
 'their',
 'woodland',
 'heights',
 'along,',
 'Shall',
 'mingle',
 'with',
 'anthems',
 'that',
 'winds',
 'and',
 'fountains',
 'sing,',
 'Till',
 'hill',
 'and',
 'valley',
 'gaily',
 'gaily',
 'ring.']
len(wordList) # total number of words
133
# number of times a word ('mountains!') is in the song?
wordList.count('mountains!')
4
# number of times a word ('gaily') is in the song?
wordList.count('gaily')
4
# number of times a word ('hill') is in the song?
wordList.count('hill')
2

CSV Files

A CSV (Comma Separated Values) file is a type of plain text file that stores tabular data. Each row of a table is a line in the text file, with each column in the row separated by commas. This format is the most common import and export format for spreadsheets and databases.

For example a simple table such as the following with column names and ages would be represented in a CSV as:

Table:

Name

Age

Harry

14

Hermoine

14

Dumbledore

60

CSV:

Name,Age
Harry,14
Hermoine,14
Dumbledore,60

We can handle csv files similar to text files and use string/list methods to process the tabular data.

Reading in Student Names

The name of students in this class are in classnames.csv in directory csv.

filename = 'csv/classnames.csv' 
with open(filename) as roster:  
    for line in roster:
        print(line.strip())
Aleman-Valencia,Karla,ka14
Batsaikhan,Munguldei,mb34
Berger,Marcello W.,mwb3
Bertolet,Jeremy S.,jsb7
Bhaskar,Monika A.,mab13
Blair,Maycie C.,mcb12
Brown,Courtney A.,cab10
Christ,Alexander M.,amc11
Gonzalez,Gabriela M.,gmg7
Herman,Adelaide A.,aah6
Hu,Jess,jhh3
Huang,Will,wh4
Jain,Divij,dj4
Kirtane,Jahnavi N.,jnk1
Kluev,Varya A.,vak1
Klugman,Pat T.,ptk2
Knight Garcia I,Grace P.,gpk1
Kolean,Owen A.,oak2
Lee,Chan,cjl5
Liang,Nathan S.,nsl3
Liu,Karen,kl14
Loftus,Andrew W.,awl5
Louchheim,Carter H.,chl2
Lurbur,Hadassah N.,hnl2
Magid,Sam P.,sm39
Miller,Jakin J.,jjm5
Moon,Chisang,cm33
O'Connor,Dan P.,dpo2
Olsen,Will T.,wto2
Paguada,Brandon,bp9
Park,Abraham S.,asp8
Polanco,Isabella G.,igp1
Poll,Noah D.,ndp2
Ratcliffe,Christopher K.,ckr1
Singh,Jaskaran,js34
Smith,Tyler C.,tcs3
Sturdevant,Zach S.,zss1
Tantum,Charlie J.,cjt3
Verkleeren,Sophia A.,sav3
Villanueva Astilleros,Paola C.,pcv1
Zhang,Alison Y.,ayz2
Anderson,Carter R.,cra3
Berger,Elissa J.,ejb5
Brissett,Keel M.,kmb9
Bruce,Giulianna,gb12
Bruns,Josh A.,jab17
Burger-Moore,Bailey C.,bcb3
Cantin,Claudia V.,cvc2
Cazabal,Victor M.,vmc3
Cecchi-Rivas,Fior D.,fdc1
Cook,Major C.,mcc8
Damra,Tala H.,thd2
Dawson,Quinn N.,qnd1
Dhingra,Ronak A.,rad6
Foisy,Sylvain J.,sjf3
Freund,Avery G.,agf1
Galizio,Riley S.,rsg2
Garcia,Kaiser A.,kag6
Gustafson,Annie H.,ahg2
Hall,Oliver E.,oeh1
Huang,Spencer B.,sbh1
Khishigsuren,Marla,mk22
Kovalski,Lola G.,lgk1
Laesch,Greta M.,gml2
Loyd,Eddie G.,egl2
Miotto,Joe,jdm9
Murray,James D.,jdm10
Nakato,Rika,rn6
Pandey,Himal R.,hrp3
Payel,Maruf,mp19
Pedlow,Harry J.,hjp2
Pujara,Shivam,smp6
Resch,Prairie C.,pcr1
Schumann,Jesse H.,jhs2
Vaccaro,William,wav1
Van Der Weide,Sebastian X.,sxv1
Wongibe,Bernard V.,bvw1

Collecting names in a list

Suppose we want to create a list of all names, where names appear in firstName (M.I.) lastName format. How do we achieve that?

students = [] # initialize empty list
filename = "csv/classNames.csv"
with open(filename) as roster: 
    for line in roster:
        fullName = line.strip().split(',')
        firstName = fullName[1]
        lastName = fullName[0]
        # print(firstName,lastName)
        students.append(firstName + ' ' + lastName)
students
['Karla Aleman-Valencia',
 'Munguldei Batsaikhan',
 'Marcello W. Berger',
 'Jeremy S. Bertolet',
 'Monika A. Bhaskar',
 'Maycie C. Blair',
 'Courtney A. Brown',
 'Alexander M. Christ',
 'Gabriela M. Gonzalez',
 'Adelaide A. Herman',
 'Jess Hu',
 'Will Huang',
 'Divij Jain',
 'Jahnavi N. Kirtane',
 'Varya A. Kluev',
 'Pat T. Klugman',
 'Grace P. Knight Garcia I',
 'Owen A. Kolean',
 'Chan Lee',
 'Nathan S. Liang',
 'Karen Liu',
 'Andrew W. Loftus',
 'Carter H. Louchheim',
 'Hadassah N. Lurbur',
 'Sam P. Magid',
 'Jakin J. Miller',
 'Chisang Moon',
 "Dan P. O'Connor",
 'Will T. Olsen',
 'Brandon Paguada',
 'Abraham S. Park',
 'Isabella G. Polanco',
 'Noah D. Poll',
 'Christopher K. Ratcliffe',
 'Jaskaran Singh',
 'Tyler C. Smith',
 'Zach S. Sturdevant',
 'Charlie J. Tantum',
 'Sophia A. Verkleeren',
 'Paola C. Villanueva Astilleros',
 'Alison Y. Zhang',
 'Carter R. Anderson',
 'Elissa J. Berger',
 'Keel M. Brissett',
 'Giulianna Bruce',
 'Josh A. Bruns',
 'Bailey C. Burger-Moore',
 'Claudia V. Cantin',
 'Victor M. Cazabal',
 'Fior D. Cecchi-Rivas',
 'Major C. Cook',
 'Tala H. Damra',
 'Quinn N. Dawson',
 'Ronak A. Dhingra',
 'Sylvain J. Foisy',
 'Avery G. Freund',
 'Riley S. Galizio',
 'Kaiser A. Garcia',
 'Annie H. Gustafson',
 'Oliver E. Hall',
 'Spencer B. Huang',
 'Marla Khishigsuren',
 'Lola G. Kovalski',
 'Greta M. Laesch',
 'Eddie G. Loyd',
 'Joe Miotto',
 'James D. Murray',
 'Rika Nakato',
 'Himal R. Pandey',
 'Maruf Payel',
 'Harry J. Pedlow',
 'Shivam Pujara',
 'Prairie C. Resch',
 'Jesse H. Schumann',
 'William Vaccaro',
 'Sebastian X. Van Der Weide',
 'Bernard V. Wongibe']

List Comprehensions to Map and Filter

When processing lists, there are common patterns that come up:

Mapping. Iterate over a list and return a new list which results from performing an operation on each element of a given list

  • E.g., take a list of integers numList and return a new list which contains the square of each number in numList

Filtering. Iterate over a list and return a new list that results from keeping those elements of the list that satisfy some condition

  • E.g., take a list of integers numList and return a new list which contains only the even numbers in numList

Python allows us to implement these patterns succinctly using list comprehensions.

numList = range(10)

# generate list of even numbers without list comprehension:
evens = []
for num in numList:
    if num % 2 == 0:
        evens.append(num)
print(evens)
[0, 2, 4, 6, 8]
# with list comprehension
# evens is a new list
numList = range(10)
evens = [num for num in numList if num % 2 == 0]
print(evens)
[0, 2, 4, 6, 8]
# mapping and filtering together
# create a list of even numbers squared
numList = range(10)
evenSquared = [num**2 for num in numList if num % 2 == 0]
print(evenSquared)
[0, 4, 16, 36, 64]

Using Functions We Built

Note: We wrote a few helper functions in the last few lectures and labs, which are now in a module called sequenceTools:

  • isVowel()

  • vowelSeq()

  • countVowels()

  • wordStartEnd()

  • palindromes()

We can import the functions from our module using the import command.

from sequenceTools import *

Student Fun Facts

Let’s use list comprehensions to answer some “student fun facts!” :-)

students = [] # initialize empty list
filename = "csv/classNames.csv"
with open(filename) as roster: 
    for line in roster:
        fullName = line.strip().split(',')
        firstName = fullName[1]
        lastName = fullName[0]
        # print(firstName,lastName)
        students.append(firstName + ' ' + lastName)

# Which student names start with a vowel?        
vowelNames = [name for name in students if isVowel(name[0])]
vowelNames
['Alexander M. Christ',
 'Adelaide A. Herman',
 'Owen A. Kolean',
 'Andrew W. Loftus',
 'Abraham S. Park',
 'Isabella G. Polanco',
 'Alison Y. Zhang',
 'Elissa J. Berger',
 'Avery G. Freund',
 'Annie H. Gustafson',
 'Oliver E. Hall',
 'Eddie G. Loyd']

More Fun Facts! Which students have long or short names?

firstNames = [name.split()[0] for name in students]
[name for name in firstNames if len(name) > 8]
['Munguldei', 'Alexander', 'Christopher', 'Giulianna', 'Sebastian']
[name for name in firstNames if len(name) < 4]
['Pat', 'Sam', 'Dan', 'Joe']

Indexing Lists of Lists

List comprehensions allow us to easily creates two-dimensional lists, or lists of lists. We can treat a list of lists just like we would a list of any other sequence (e.g. strings).

To index an element of an inner list, we’d need two indices:

  • first (leftmost) index identifies which inner list we want, and

  • second (rightmost) index identifies which element within that inner list we want.

filename = 'csv/classnames.csv' 
allStudents = []
with open(filename) as roster:
    for student in roster:
        allStudents.append(student.strip().split(','))
allStudents # list of list of strings
[['Aleman-Valencia', 'Karla', 'ka14'],
 ['Batsaikhan', 'Munguldei', 'mb34'],
 ['Berger', 'Marcello W.', 'mwb3'],
 ['Bertolet', 'Jeremy S.', 'jsb7'],
 ['Bhaskar', 'Monika A.', 'mab13'],
 ['Blair', 'Maycie C.', 'mcb12'],
 ['Brown', 'Courtney A.', 'cab10'],
 ['Christ', 'Alexander M.', 'amc11'],
 ['Gonzalez', 'Gabriela M.', 'gmg7'],
 ['Herman', 'Adelaide A.', 'aah6'],
 ['Hu', 'Jess', 'jhh3'],
 ['Huang', 'Will', 'wh4'],
 ['Jain', 'Divij', 'dj4'],
 ['Kirtane', 'Jahnavi N.', 'jnk1'],
 ['Kluev', 'Varya A.', 'vak1'],
 ['Klugman', 'Pat T.', 'ptk2'],
 ['Knight Garcia I', 'Grace P.', 'gpk1'],
 ['Kolean', 'Owen A.', 'oak2'],
 ['Lee', 'Chan', 'cjl5'],
 ['Liang', 'Nathan S.', 'nsl3'],
 ['Liu', 'Karen', 'kl14'],
 ['Loftus', 'Andrew W.', 'awl5'],
 ['Louchheim', 'Carter H.', 'chl2'],
 ['Lurbur', 'Hadassah N.', 'hnl2'],
 ['Magid', 'Sam P.', 'sm39'],
 ['Miller', 'Jakin J.', 'jjm5'],
 ['Moon', 'Chisang', 'cm33'],
 ["O'Connor", 'Dan P.', 'dpo2'],
 ['Olsen', 'Will T.', 'wto2'],
 ['Paguada', 'Brandon', 'bp9'],
 ['Park', 'Abraham S.', 'asp8'],
 ['Polanco', 'Isabella G.', 'igp1'],
 ['Poll', 'Noah D.', 'ndp2'],
 ['Ratcliffe', 'Christopher K.', 'ckr1'],
 ['Singh', 'Jaskaran', 'js34'],
 ['Smith', 'Tyler C.', 'tcs3'],
 ['Sturdevant', 'Zach S.', 'zss1'],
 ['Tantum', 'Charlie J.', 'cjt3'],
 ['Verkleeren', 'Sophia A.', 'sav3'],
 ['Villanueva Astilleros', 'Paola C.', 'pcv1'],
 ['Zhang', 'Alison Y.', 'ayz2'],
 ['Anderson', 'Carter R.', 'cra3'],
 ['Berger', 'Elissa J.', 'ejb5'],
 ['Brissett', 'Keel M.', 'kmb9'],
 ['Bruce', 'Giulianna', 'gb12'],
 ['Bruns', 'Josh A.', 'jab17'],
 ['Burger-Moore', 'Bailey C.', 'bcb3'],
 ['Cantin', 'Claudia V.', 'cvc2'],
 ['Cazabal', 'Victor M.', 'vmc3'],
 ['Cecchi-Rivas', 'Fior D.', 'fdc1'],
 ['Cook', 'Major C.', 'mcc8'],
 ['Damra', 'Tala H.', 'thd2'],
 ['Dawson', 'Quinn N.', 'qnd1'],
 ['Dhingra', 'Ronak A.', 'rad6'],
 ['Foisy', 'Sylvain J.', 'sjf3'],
 ['Freund', 'Avery G.', 'agf1'],
 ['Galizio', 'Riley S.', 'rsg2'],
 ['Garcia', 'Kaiser A.', 'kag6'],
 ['Gustafson', 'Annie H.', 'ahg2'],
 ['Hall', 'Oliver E.', 'oeh1'],
 ['Huang', 'Spencer B.', 'sbh1'],
 ['Khishigsuren', 'Marla', 'mk22'],
 ['Kovalski', 'Lola G.', 'lgk1'],
 ['Laesch', 'Greta M.', 'gml2'],
 ['Loyd', 'Eddie G.', 'egl2'],
 ['Miotto', 'Joe', 'jdm9'],
 ['Murray', 'James D.', 'jdm10'],
 ['Nakato', 'Rika', 'rn6'],
 ['Pandey', 'Himal R.', 'hrp3'],
 ['Payel', 'Maruf', 'mp19'],
 ['Pedlow', 'Harry J.', 'hjp2'],
 ['Pujara', 'Shivam', 'smp6'],
 ['Resch', 'Prairie C.', 'pcr1'],
 ['Schumann', 'Jesse H.', 'jhs2'],
 ['Vaccaro', 'William', 'wav1'],
 ['Van Der Weide', 'Sebastian X.', 'sxv1'],
 ['Wongibe', 'Bernard V.', 'bvw1']]
allStudents[0] # list of first student's info
['Aleman-Valencia', 'Karla', 'ka14']
allStudents[0][1] # how do I index Karla's first name?
'Karla'
# now with a list comprehension
filename = 'csv/classnames.csv' 
with open(filename) as roster:
    allStudents = [student.strip().split(',') for student in roster] 
allStudents # a list of lists of strings!
[['Aleman-Valencia', 'Karla', 'ka14'],
 ['Batsaikhan', 'Munguldei', 'mb34'],
 ['Berger', 'Marcello W.', 'mwb3'],
 ['Bertolet', 'Jeremy S.', 'jsb7'],
 ['Bhaskar', 'Monika A.', 'mab13'],
 ['Blair', 'Maycie C.', 'mcb12'],
 ['Brown', 'Courtney A.', 'cab10'],
 ['Christ', 'Alexander M.', 'amc11'],
 ['Gonzalez', 'Gabriela M.', 'gmg7'],
 ['Herman', 'Adelaide A.', 'aah6'],
 ['Hu', 'Jess', 'jhh3'],
 ['Huang', 'Will', 'wh4'],
 ['Jain', 'Divij', 'dj4'],
 ['Kirtane', 'Jahnavi N.', 'jnk1'],
 ['Kluev', 'Varya A.', 'vak1'],
 ['Klugman', 'Pat T.', 'ptk2'],
 ['Knight Garcia I', 'Grace P.', 'gpk1'],
 ['Kolean', 'Owen A.', 'oak2'],
 ['Lee', 'Chan', 'cjl5'],
 ['Liang', 'Nathan S.', 'nsl3'],
 ['Liu', 'Karen', 'kl14'],
 ['Loftus', 'Andrew W.', 'awl5'],
 ['Louchheim', 'Carter H.', 'chl2'],
 ['Lurbur', 'Hadassah N.', 'hnl2'],
 ['Magid', 'Sam P.', 'sm39'],
 ['Miller', 'Jakin J.', 'jjm5'],
 ['Moon', 'Chisang', 'cm33'],
 ["O'Connor", 'Dan P.', 'dpo2'],
 ['Olsen', 'Will T.', 'wto2'],
 ['Paguada', 'Brandon', 'bp9'],
 ['Park', 'Abraham S.', 'asp8'],
 ['Polanco', 'Isabella G.', 'igp1'],
 ['Poll', 'Noah D.', 'ndp2'],
 ['Ratcliffe', 'Christopher K.', 'ckr1'],
 ['Singh', 'Jaskaran', 'js34'],
 ['Smith', 'Tyler C.', 'tcs3'],
 ['Sturdevant', 'Zach S.', 'zss1'],
 ['Tantum', 'Charlie J.', 'cjt3'],
 ['Verkleeren', 'Sophia A.', 'sav3'],
 ['Villanueva Astilleros', 'Paola C.', 'pcv1'],
 ['Zhang', 'Alison Y.', 'ayz2'],
 ['Anderson', 'Carter R.', 'cra3'],
 ['Berger', 'Elissa J.', 'ejb5'],
 ['Brissett', 'Keel M.', 'kmb9'],
 ['Bruce', 'Giulianna', 'gb12'],
 ['Bruns', 'Josh A.', 'jab17'],
 ['Burger-Moore', 'Bailey C.', 'bcb3'],
 ['Cantin', 'Claudia V.', 'cvc2'],
 ['Cazabal', 'Victor M.', 'vmc3'],
 ['Cecchi-Rivas', 'Fior D.', 'fdc1'],
 ['Cook', 'Major C.', 'mcc8'],
 ['Damra', 'Tala H.', 'thd2'],
 ['Dawson', 'Quinn N.', 'qnd1'],
 ['Dhingra', 'Ronak A.', 'rad6'],
 ['Foisy', 'Sylvain J.', 'sjf3'],
 ['Freund', 'Avery G.', 'agf1'],
 ['Galizio', 'Riley S.', 'rsg2'],
 ['Garcia', 'Kaiser A.', 'kag6'],
 ['Gustafson', 'Annie H.', 'ahg2'],
 ['Hall', 'Oliver E.', 'oeh1'],
 ['Huang', 'Spencer B.', 'sbh1'],
 ['Khishigsuren', 'Marla', 'mk22'],
 ['Kovalski', 'Lola G.', 'lgk1'],
 ['Laesch', 'Greta M.', 'gml2'],
 ['Loyd', 'Eddie G.', 'egl2'],
 ['Miotto', 'Joe', 'jdm9'],
 ['Murray', 'James D.', 'jdm10'],
 ['Nakato', 'Rika', 'rn6'],
 ['Pandey', 'Himal R.', 'hrp3'],
 ['Payel', 'Maruf', 'mp19'],
 ['Pedlow', 'Harry J.', 'hjp2'],
 ['Pujara', 'Shivam', 'smp6'],
 ['Resch', 'Prairie C.', 'pcr1'],
 ['Schumann', 'Jesse H.', 'jhs2'],
 ['Vaccaro', 'William', 'wav1'],
 ['Van Der Weide', 'Sebastian X.', 'sxv1'],
 ['Wongibe', 'Bernard V.', 'bvw1']]
len(allStudents) # number of students in class
77

Aside: Generating random indices. Python gives us a convenient way to generate random numbers using the random module.

import random # import module to help generate random numbers
randomIndex = random.randint(0, 76)  
# generates a random integer between 0 and 76 (inclusive!!)
randomIndex = random.randint(0, 76)  
allStudents[randomIndex] # great way to cold call!
['Nakato', 'Rika', 'rn6']
allStudents[random.randint(0,76)][1]   
# Accessing just the name
'Harry J.'

Class Exercises

  1. Give our list of lists of strings shown above, write a function that returns a list of all students’ last names? Hint: Use a list comprehension.

def lastNames(rosterList):
    """Takes the student info as a list of lists and returns just
    a list of last names"""
    pass
    
  1. Write a function characterList which takes in two arguments rosterList (list of lists) and character (a string) and returns the list of students in the class whose name starts with character.

def characterList(rosterList, character):
    """Takes the student info as a list of lists and a string character
    and returns a list of students whose name starts with character"""
    pass
characterList(allStudents, "B") 
  1. Write a function that computes the student with the most vowels in their last name. (Hint: use countVowels().)

def mostVowels(rosterList):
    """Takes the student info as a list of lists of strings
    and returns the student name with the most vowels in their last name"""
    pass
mostVowels(allStudents)