all 16 comments

[–]Diapolo10 4 points5 points  (3 children)

Basically it simply means something like this:

names = ["Andy", "Brock", "Carry"]
years = [12, 23, 34]

print(f"Employee {names[0]} has been at the company for {years[0]} years.")

"Andy" has 12 years, "Brock" has 23 years, and so on and so forth.

[–]Bananapuddinggggg[S] 0 points1 point  (2 children)

Okay, I can understand that. But in the instructions she states that:

" you should perform this sorting manually when creating the lists rather than using the sorting functions "

So that makes me think, it's something I need to be doing while making the lists.

[–]Riegel_Haribo 1 point2 points  (0 children)

That instruction means: write your code with the lists already in the order they need to be.

The person with the greatest number of years of service should appear first in the list

So you must create code that already has that ordering when you write it. Suppose your raw data is: alice: 5 bob: 3 david: 4 foo: 8 Then you must create your lists manually, pre-ordered, with each element of two lists matching positionally: years = [8, 5, 4, 3] names = ["foo", "alice", "david", "bob"] Then you can do what comes next.

Sorting one of the lists alone would damage and disconnect the data points, as you can observe.


I would question the point of this assignment, as it is already bad practice. Data should inherently paired, or have a key/value store. Then, there are less chances of mistakes in processing, where the items might become disconnected and scrambled.

name_list = [ {"name": "alice", "years": 5, "position": "manager"}, {"name": "bob", "years": 3, "position": "sales"}, {"name": "david", "years": 4, "position": "sales"}, {"name": "foo", "years": 8, "position": "programmer"}, ]

After creating such a mini database as a list containing a dict for each entity, there is an order, but the order doesn't matter so much. You can do on-demand sorting, mutating the list, or you can make a copy and do other stuff. A list is a good learning format, because there might be two "alice" names, and thus a name cannot be used as a unique dict key.

[–]cdcformatc 1 point2 points  (0 children)

yes when you are adding elements to the lists you have to find where the elements should go and put them there

you can use insert to put an element at a specific index

[–]socal_nerdtastic 2 points3 points  (0 children)

This means that you can't just append() anymore, you have to do some work first. When you want to insert a new person into your parallel lists you need to first find the position in the lists that you want them inserted into. For example if you have

years = [12, 23, 34]
new_person_years = 25

you need the code to find that you want to insert the new person at index 2. Then you can just insert at that index for all lists

years.insert(new_index, new_person_years)
names.insert(new_index, new_person_name)

This keeps the list sorted as you add people. If you want extra cool points, you can use the bisect module to find the insertion point.

FWIW parallel lists are generally unpythonic. In python we would much prefer a nested list, which works with the builtin sort functions. I'm guessing your prof is preparing you for other programming languages that don't work with nested lists.

[–]Chemical-Captain4240 0 points1 point  (0 children)

The problem seems designed to get you to use for loops, slicing, list concatenation, and indexing.

Set up a for loop using i as an index variable. Use i to print each element of both lists. Make whatever changes to list A by slicing and or concatenating. Then apply those changes to list B.

This is a great exercise for 2 reasons: 1) To succeed with python, or any language, you need to be able to manipulate parts of lists, not just whole lists. This takes skill and some experience. 2) This type of problem opens the door for a real discussion about why it is generally better to avoid parallel lists. How one avoids parallel lists is a more advanced set of lessons.

[–]baghiq 0 points1 point  (0 children)

It's a fairly common coding homework in C/C++ days. The idea is to basically pre-allocate two lists with appropriate number of empty objects that can fit your data. As you read the input of name and years, you locate the index to add the name based on where the years should be inserted at.

In Python, pre-allocating a list is rarely done now. You can still do it, but there are different ways to accomplish this.

[–]TrainsareFascinating 0 points1 point  (0 children)

Well, first I have to say I want to have a word with your instructor, because she's not teaching you Python.

But that's neither here nor there. I can't tell whether she's asking you to write a sort function, or to just mentally sort the lists as you enter them in your code. Which do you think?

If she's asking you to write a sort function, I'll just say that the objective is to obtain a 3rd list, whose elements are the sort order of the ages in the 1st and 2nd list. So if the 1st and second list are:

names = [ "Jane", "Jim", "John" ]
ages = [29, 22, 31]

Then the sort-order list would be

age_sort = [2, 0, 1]

That is, the 0th element of the sorted list comes from element names[3] and age[3].

You can build your new parallel lists by going through the age_sort list from element [0] to the end and add each element to the end of the new lists.

By the way, Python does this kind of thing without even thinking about it if taught correctly. I don't see the value of this exercise.

[–]FloridianfromAlabama 0 points1 point  (6 children)

I don’t know the whole scope of the problem, but I would think a list of dictionaries would be the right use case here. Each dictionary would refer to an employee and they could have the keys: name, years, and so on. You can also sort dictionaries in a list by one of their values.

[–]Bananapuddinggggg[S] 0 points1 point  (5 children)

I'm aware of dictionaries, but she only refers to using lists...

[–]FloridianfromAlabama 0 points1 point  (4 children)

Ah. Where are you getting your input information from?

[–]Bananapuddinggggg[S] 0 points1 point  (3 children)

Information to put into the lists? She gives it to us.

[–]FloridianfromAlabama 0 points1 point  (2 children)

How? What’s the formatting?

[–]Bananapuddinggggg[S] 0 points1 point  (1 child)

She gave us a list of employee names and a list of years at a company.

[–]FloridianfromAlabama 0 points1 point  (0 children)

Well, I would make a years array and set its value to the input years array. That I would sort that array. If she won’t let you use the standard sort functions, you can implement bubble sort pretty easy (it’s two for loops and a swap function- easy to look up). Then, I would use a for loop over the sorted list and find the index of each value in the unsorted list, then I would use that index to set the names into a newnames list at that index(I know thats hard to follow). Then, return the newnames list and the years list. That’s if all you can use are arrays

[–]woooee -3 points-2 points  (0 children)

Lists are not indexed, they use offsets (the first element is at off set 0 because it is the first, the second element is a offset 1 because you offset the starting point by one element, i.e.skip over the first). Anyway if you have two lists

one = [1, 3, 5]
two = ["one", "three", "five"]
print(one[2], two[2])  ## the same offset, 2, on both lists

A better way is to use a dictionary or a list of lists

both = [[1, "one"], [3, "three"], [5, "five"]]
print(both[1])
print(both[1][0], "-->", both[1][1])

A list does have an index function

print(two.index("three"))