Using a list of lists as a lookup table and updating a value in new list of lists

Related searches

I have an application that creates a list of lists. The second element in the list needs to be assigned using lookup list which also consists of a list of lists.

I have used the "all" method to match the values in the list. If the list value exists in the lookup list, it should update the second position element in the new list. However this is not the case. The == comparative yields a False match for all elements, even though they all exist in both lists.

I have also tried various combinations of index finding commands but they are not able to unpack the values of each list.

My code is below. The goal is to replace the "xxx" values in the newData with the numbers in the lookupList.

lookupList= [['Garry','34'],['Simon', '24'] ,['Louise','13'] ]

newData = [['Louise','xxx'],['Garry', 'xxx'] ,['Simon','xxx'] ]  

#Matching values
for i in newData: 
    if (all(i[0] == elem[0] for elem in lookupList)):
        i[1] = elem[1]

You can't do what you want with all(), because elem is not a local variable outside of the generator expression.

Instead of using a list, use a dictionary to store the lookupList:

lookupDict = dict(lookupList)

and looking up matches is a simple constant-time (fast) lookup:

for entry in newData:
    if entry[0] in lookupDict:
        entry[1] = lookupDict[entry[0]]

5. Dictionaries - Q for Mortals - Code at Kx, The second element in the list needs to be assigned using lookup list which also consists of a list of lists. I have used the "all" method to match the values in the´┐Ż Here is a somewhat long VLOOKUP formula that can deal with the job. Select a cell below the criterion which you want to create the unique list based on, type this formula =IFERROR(INDEX(B$1:B$13, MATCH(0, COUNTIF(D$1:D1, IF(A$1:A$13=D$1,B$1:B$13,D$1)), 0)),""), press Shift + Ctrl + Enter keys to get the correct value, and then drag fill handle down until blank cell appears.

you should use dictionaries instead, like this:

lookupList = newData = {}

old_lookupList = [['Garry','34'],['Simon', '24'] ,['Louise','13'] ]
old_newData = [['Louise','xxx'],['Garry', 'xxx'] ,['Simon','xxx'] ] 

#convert into dictionary
for e in old_newData: newData[e[0]] = e[1]
for e in old_lookupList: lookupList[e[0]] = e[1]

#Matching values
for key in lookupList:
    if key in newData.keys():
        newData[key]=lookupList[key]

#convert into list
output_list = []

for x in newData:
    output_list.append([x, newData[x]])

How to create two advanced dynamic lists in Excel, A dictionary is an association between a list of keys and a list of values. Observe that the q console displays a dictionary as an I/O table. As with lists, key lookup in a dictionary is extended item-wise to a simple list of keys. when k is in key d , or inserts (appends) a new key-value association when k is not in key d . Updating a list using copy/paste is a boring task. This blog article describes how to update values in a price list with new values. Sheet 1 is the old price list. It contains 5000 products and amounts. Sheet2 is the new price list. It contains 2000 random products from the old price list with new prices. (There are no new products)

I like the following code since it can be tweaked and used in different ways:

lookupList= [ ['Garry', '34'],['Simon', '24'] ,['Louise', '13'] ]

newData = [ ['Louise', 'xxx'],['Garry', 'xxx'], ['Peter', 'xxx']  ,['Simon', 'xxx'] ]  

#Matching values
for R in newData:
    for i in range(0, len(lookupList) + 1):
        try:
            if lookupList[i][0] == R[0]:
                R[1] = lookupList[i][1]
                break
        except:
            print('Lookup fail on record:', R) 

print(newData)

Create list relationships by using unique and lookup columns , Advanced list solutions are easy thanks to Excel's Table object. show you two advanced list features using a validation list and a lookup function to validation lists and Table objects, but inserting new data into a Table is We have a data set in columns B through E and a list of unique shelf code values. However, when I added new entries to the table and saved the changes instead of updating the "Components" list, a new list "Components_1" was created. In theory, I could delete the old "Components" list and rename the new "Components_1". However, I have a picture library with a lookup table that links items and components to their pictures.

20. Dictionaries — How to Think Like a Computer Scientist: Learning , Learn how to create relationships between lists by using a combination of unique up" the ID column in the target list (Courses) based on a matching value. A Lookup column is connected to a SharePoint list for its set of values. If a new department is added to the Departments list, the values in the connected lookup column are updated automatically. Using this approach provides SharePoint users with a consistent and relevant set of values.

All of the compound data types we have studied in detail so far — strings, lists, a dictionary named eng2sp; the other assignments add new key:value pairs to the dictionary. Another way to create a dictionary is to provide a list of key:value pairs using Dictionaries provide an elegant way to generate a frequency table:. I have two lists A and B. List A has a lookup column to associate a record with a record from list B. When a record is created in list A I need to updated a Status column in list B. I've started the Flow with: A trigger - New Item Created Get Item - from the same list as in point 1 using the ID

Searches for a value in the first column of a table array and returns a value in the same row from another column (to the right) in the table array. Formula breakdown: =VLOOKUP(lookup_value, table_array, col_index_num, [range_lookup]) What it means: =VLOOKUP(this value, in this list, and get me value in this column, Exact Match/FALSE/0])

Comments
  • Why not just use a dictionary instead?
  • @MartijnPieters I was considering this but seeing as my application is producing a list of lists anyway then I thought this might be the best option. Also the strings that are generated can be quite verbose and I am not sure how this would influence a dictionary. I will also be working with several hundred lists in each list. Is a dictionary the most effective way to handle this?
  • Absolutely, without a doubt. There won't be any new string objects; the dictionary you'd create from the list references the same string objects anyway.
  • @MartijnPieters Ok thanks, I think my hesitation of using a dictionary stems from my own mental image of a dictionary. If there is no limit to the string length of keys used in a dictionary then this must be the best solution.