Parallel Lists Help

Posted on 2013-10-26
Last Modified: 2013-10-29
Hello Experts,

I am having a bit of trouble again with my homework. the basic premise is I am supposed to write a program that uses parallel lists that will hold the name of a person and then their salary, then display the average salary the highest salary with persons name and then the lowest salary with the persons name. where I am stuck is the output of highest salary with person name and lowest salary with person name.

so this is what I have thus far:

numemployees = 0

numemployees = int(input("how many employees do you have? "))
if numemployees <= 0:
    print(" you have entered an invalid number!")

employees = [" "] * numemployees
salaries = [0] * numemployees
n = 0
sum = 0.0
average = 0.0

for n in range(0,numemployees):
    print("eneter the name", (n + 1))
    employees[n] = str(input())
for n in range(0,numemployees):
    print("enter the employees salary", (n + 1))
    salaries[n] = int(input())
    sum = (sum + salaries[n])
    if salaries[n] <=0 or salaries[n] >= 200000:
        print("you have entered an invalid amount try again")
        sum = (sum - salaries[n])
        print("enter the employees salary", (n - 1))
        salaries[n] = int(input())
average = (sum/numemployees)
print(" the average salary is",average)

Open in new window

It function the way i expect it to i think... maybe it's all wrong.

anyway the problem I am running into is here:
print("the highest salary is",max(salaries),employees)
print("the Lowest salary is", min(salaries),employees)

Open in new window

this is where it displays the highest and lowest salaries and the entire list of names, like so:

the average salary is 666.6666666666666
the highest salary is 900 ['bob', 'jim', 'joe']
the Lowest salary is 500 ['bob', 'jim', 'joe']

I have include the input I used to get the above output in the attachment:

the out put should look something like:
the average salary is 666.6666666666666
the highest salary is 900 joe
the Lowest salary is 500 jim

I appreciate any bit of help you can give.

Thanks you and best regards,
Question by:b_zajac
  • 4
  • 4
LVL 28

Accepted Solution

pepr earned 500 total points
ID: 39603709
The employees is a list; hence, the list representation is printed. Think as if having paper with one column of names and second column of numbers (salaries for the names). The max gives you the maximum of the salary values, but it will not tell you on which line it was found.

There are two principal solutions: 1) you will remember index of the minimum and maximum values (and update the dedicated index variable) during input, or 2) you will find the index of the list element with the wanted value later. The name is stored at the same index in the parallel list.

There is the .index() method for sequence types -- i.e. also for lists. See

In my opinion, the second approach is more general. The same way you can postpone calculation of the average to the time after the input.

Author Comment

ID: 39603933
I guess I just don't understand the documentation, I look at this:

s.index(x[, i[, j]])	index of the first occurrence of x in s (at or after index i and before index j)	(8)

Open in new window

and it means nothing to me and the book is just as cryptic. I just don't understand I guess.

When you say index at the end like finding the value do you mean something like.

index = index + 1 (#-> in each for statement) 

#Output like this
print("the highest salary is",max(salaries),employees[index])
print("the Lowest salary is", min(salaries),employees[index])

Open in new window

LVL 28

Assisted Solution

pepr earned 500 total points
ID: 39606790
The list type or the string type or bytearray or similar are considered sequence types. The .index() method is available to each of them. It has nothing to do with your index variable.

The [ ] brackets in the doc are used to express optional usage of the extra arguments.

If you save say max salary value like mx = max(salaries), then i = salaries.index(mx) return the position of the value in the list, and the employees[ i ] is the name of the person with the maximum salary. Is it more understandable?

Author Comment

ID: 39606856
so would it look something like this?

mx = max(salaries)
mn = min(salaries)
i = salaries.index(mx)
j = salaries.index(mn)

print(mx, employees[i])

Open in new window

do i have to count the index like

index = 0
index = index + 1

Open in new window

I did not mean to sound belligerent in my last reply, I am very frustrated that i am haveing such a difficult time wrapping my head around this concept. Thank you for trying to help me understand this concept.
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

LVL 28

Expert Comment

ID: 39607242
Yes, the first part of your last comment should be OK. There also is a standard built-in function named sum() that can be user for summing the salaries. That function contain a hidden loop inside. Basically you can do it using the for loop. You can also use the for loop when searching the index of the max value element. Using the .index() method is just easier, more brief, and less error prone.

Just ask what makes you trouble. You definitely need to understand all details if you want to understand more complex things later.

Author Comment

ID: 39607376
I will defiantly be seeking more info on this topic both from my instructor, corse workbook and the python cookbook i just purchased. I really appreciate the time you have taken out of your schedule to explain it to me.

Author Closing Comment

ID: 39607393
I think i get it now. .index() is built in and will keep track of all list indexes if they are parallel. and using a variable like in the examples above can be call at the end of one list and then used to coordinate that same indexed place in the adjacent list.
LVL 28

Expert Comment

ID: 39608029
I think i get it now. .index() is built in and will keep track of all list indexes if they are parallel.

No. This is not the case. Study the comments and the code below

# Here are the parallel lists.
employees = ['bob', 'jim', 'joe']
salaries =  [600, 500, 900]

# You can index each list separately. When using the same index, you get
# the related values. But the lists are not bound technically. It is only
# the agreement that the elements on the same index value. The index value
# is zero based.
print(employees[0], salaries[0])
print(employees[1], salaries[1])
print(employees[2], salaries[2])

# You can loop through the lists. The enumerate() function will add the index
# value.
print('-' * 50)
for i, name in enumerate(employees):
    print('index:', i, '\tname:', name)

# You can do the same with the salaries.
print('-' * 50)
for i, salary in enumerate(salaries):
    print('index:', i, '\tsalary:', salary)

print('-' * 50)

# The .index() method just return the index value of the element that contains
# the given value.
print('index for bob:', employees.index('bob'))
print('index for joe:', employees.index('joe'))

# Let's try .index() with the value that is not in the list. It causes
# exception to indicate the case. The exception can be caught so that
# the programmer can react to the situation.
   print('index for nobody:', employees.index('nobody'))
except ValueError as e:
   print('The name was not found in the list.')
   # If you do not use the try-except construct in such case, the program
   # stops and displays the message like... (the .__str__() method does that).
   print('Python would tell you:', e)

# Whe the exception does not happen, it behaves as if only the commands between
# try and except existed. This way, you can always used the try-except when you
# know that something wrong can happen (but not neccessarily). Here for jim.
   print('index for jim:', employees.index('jim'))
except ValueError as e:
   print('Some problem.', e)

# You tried and you have learned things. Because of that I am showing
# the alternative solution. The sum() built-in function is capable to
# sum the elements. You are already familiar with the max() and min().
# The len() function returns the length of a sequence (number of elements).
print('sum:', sum(salaries))
print('len:', len(salaries))
print('avg:', sum(salaries)/len(salaries))

min_salary = min(salaries)
max_salary = max(salaries)

print('min:', min_salary)
print('max:', max_salary)

# Now search for the index of the element with minimum salary.
imin = salaries.index(min_salary)
print('i min:', imin)

# The same for maximum.
imax = salaries.index(max_salary)
print('i max:', imax)

# The question for you: Why the try-except construct need not to be used
# in this case?

# There is the agreement that the element with the same index contains
# the name in the other list.
print('Employee with the min salary:', employees[imin])
print('Employee with the max salary:', employees[imax])

Open in new window

It prints on my console:
bob 600
jim 500
joe 900
index: 0        name: bob
index: 1        name: jim
index: 2        name: joe
index: 0        salary: 600
index: 1        salary: 500
index: 2        salary: 900
index for bob: 0
index for joe: 2
The name was not found in the list.
Python would tell you: 'nobody' is not in list
index for jim: 1
sum: 2000
len: 3
avg: 666.6666666666666
min: 500
max: 900
i min: 1
i max: 2
Employee with the min salary: jim
Employee with the max salary: joe

Open in new window


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now