Jim T
Jim T

Reputation: 65

Append to end of list if element matches element in list of lists

I have a list of lists in which I want to match the [3] element in each nested list to the [1] element in another set of lists generated from a for loop. The [1] element should be appended to each nested list of lists if there is a match.

unique_data = [[98764, 'Feynman, R', 'SFEN', 'SSW 687'], [98763, 'Newton, I', 'SFEN', 'SSW 689'], [98763, 'Newton, I', 'SFEN', 'SSW 555']

Student count generated for each course from a for loop:

['SSW 555', 1]
['SSW 689', 1]
['SSW 687', 3]

Could a good approach be generating a list of lists from the student counts?

This approach (where coursecount is each one of the generated lists):

for i in unique_data:
    if coursecount[0] == [el[3] for el in unique_data]:
        unique_data.append(coursecount[1])

Doesn't work because [el[3] for el in unique_data] results in a printout of a list of all the courses, which of course is not going to match a single value. End desired result would be something like:

[98764, 'Feynman, R', 'SFEN', 'SSW 687', 3]
[98763, 'Newton, I', 'SFEN', 'SSW 689', 1]
[98763, 'Newton, I', 'SFEN', 'SSW 555', 1]

Upvotes: 0

Views: 793

Answers (3)

Threem
Threem

Reputation: 134

This sample produces what you wanted:

listsss = [['SSW 555', 1], ['SSW 689', 1], ['SSW 687', 3]]
unique_data = [[98764, 'Feynman, R', 'SFEN', 'SSW 687'], [98763, 'Newton, I', 'SFEN', 'SSW 689'], [98763, 'Newton, I', 'SFEN', 'SSW 555']]
    
for coursecount in listsss:
    for element in unique_data:
        if coursecount[0] == element[3]:
            element.append(coursecount[1])
print(unique_data)

Upvotes: 0

pho
pho

Reputation: 25489

coursecount looks like a list of lists. This data structure isn't very efficient if you want to look up things from it. You'd be better off converting this to a dictionary.

coursecount = [['SSW 555', 1], ['SSW 689', 1], ['SSW 687', 3]]
coursecount_dict = {a: b for a, b in coursecount}
# Or simply, you could do
# coursecount_dict = dict(coursecount)
# because coursecount is a Nx2 list of key-value pairs

Now coursecount_dict is

{'SSW 555': 1, 'SSW 689': 1, 'SSW 687': 3}

You can look up values in a dictionary using the key much, much faster than you can look up items in a list-of-lists. For example, to get the value for 'SSW 687', all you'd need to do is coursecount_dict['SSW 687']. This is an O(1) operation because of the way dictionaries work. If you used a list-of-lists, this would be an O(n) operation because you'd have to iterate over all the courses to find the one that matched.

Then, you want to loop over unique_data, get the correct item from the dictionary, and append it to the list in unique_data. You can append it in-place.

unique_data = [[98764, 'Feynman, R', 'SFEN', 'SSW 687'], [98763, 'Newton, I', 'SFEN', 'SSW 689'], [98763, 'Newton, I', 'SFEN', 'SSW 555']]

for item in unique_data:
    coursename = item[3]
    students = coursecount_dict.get(coursename, 0) 
    # students becomes coursecount_dict[coursename], 0 if not exists
    item.append(students)

Now unique_data becomes:

[[98764, 'Feynman, R', 'SFEN', 'SSW 687', 3],
 [98763, 'Newton, I', 'SFEN', 'SSW 689', 1],
 [98763, 'Newton, I', 'SFEN', 'SSW 555', 1]]

Upvotes: 1

Renaud
Renaud

Reputation: 2819

I think:

result_list=[] 
for i in unique_data:
    if coursecount[0] == i[3]:
        Result_list.extend(i.append(coursecount[1]))
        

Upvotes: 0

Related Questions