Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop through List
#1
Good morning, I can really use some helpm

I need to iterate over a list and if the criteria is met, add item to list. Then print list with the values. Here is what I have:
The code up until the end works as expected.

# After reading the data, 
# a. Print the state with the highest mean Verbal SAT score 
# b. Print each state that has a mean Math SAT score greater than 500



satfile = open('state_satscores_2004.txt','r')
count = 0
SATList = []

for line in satfile:
    count += 1
    textline = line.strip()
    items = textline.split()
    SATList.append(items)
        

# print the number of teams read
print('Number of Lines:', count)
print(SATList)
print('\n','\n')

state = []
for line in SATList:
     state.append(line[0])
print('Here is your state list')
print(state)
print('\n','\n')


verbal = []
for line in SATList:
     verbal.append(line[1])
print('Here is your verbal list')
print(verbal)
print('\n','\n')


math = []
for line in SATList:
     math.append(line[2])
print('Here is your Math list')
print(math)
print('\n','\n')


max_verbal = max(verbal)
max_state = state[verbal.index(max_verbal)]
print('The state with the highest mean Verbal SAT score is:',max_state,'With a score of:',max_verbal)
print('\n','\n')



#THIS IS THE PART I NEED HELP WITH
stategrades = []
goodgrades = []
print('Here are your good grades')
for i,j in SATList:
    if math[2] > str(500):
        stategrades.append(i)
        goodgrades.append(j)
print(stategrades)
Thank you for any help!!
Reply


Messages In This Thread
Loop through List - by jeran042 - Jul-30-2019, 12:17 PM
RE: Loop through List - by ichabod801 - Jul-30-2019, 12:51 PM
RE: Loop through List - by jeran042 - Jul-30-2019, 02:58 PM
RE: Loop through List - by ichabod801 - Jul-30-2019, 03:17 PM
RE: Loop through List - by jeran042 - Jul-30-2019, 04:22 PM
RE: Loop through List - by ichabod801 - Jul-30-2019, 05:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Appending to list of list in For loop nico_mnbl 2 2,487 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl
  Append list into list within a for loop rama27 2 2,543 Jul-21-2020, 04:49 AM
Last Post: deanhystad
  loop through list or double loop 3Pinter 4 3,596 Dec-05-2018, 06:17 AM
Last Post: 3Pinter
  Write a for loop on list of lists without changing the shape of the main list Antonio 3 3,925 Jun-19-2018, 02:16 AM
Last Post: ichabod801
  For looping over a list, editing the list from inside the loop? Krookroo 3 4,131 Sep-04-2017, 05:08 PM
Last Post: Krookroo
  How to change from printFacts ( ) to return a list & Loop over list when writing CSV Ivan1 14 8,716 Aug-30-2017, 12:14 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020