Adding values from one list to another when they share value

how to import data from one google sheet to another
how do i link data from one google spreadsheet to another?
how to transfer data from one google sheet to another automatically
google sheets pull data from another sheet based on criteria
python dictionary with list as value
python dictionary append
google sheets reference another workbook
google sheets importrange from another tab

I'm trying to add values from List2 if the type is the same in List1. All the data is strings within lists. This isn't the exact data I'm using, just a representation. This is my first programme so please excuse any misunderstandings.

List1 = [['Type A =', 'Value 1', 'Value 2', 'Value 3'], ['Type B =', 'Value 4', 'Value 5']]
List2 = [['Type Z =', 'Value 6', 'Value 7', 'Value 8'], ['Type A =', 'Value 9', 'Value 10', 'Value 11'], ['Type A =', 'Value 12', 'Value 13']]

Desired result:

new_list =[['Type A =', 'Value 1', 'Value 2', 'Value 3', 'Value 9', 'Value 10', 'Value 11', 'Value 12', 'Value 13'], ['Type B =', 'Value 4', 'Value 5']]

Current attempt:

newlist = []
for values in List1:
    for valuestoadd in List2:
        if values[0] == valuestoadd[0]:
            newlist = [List1 + [valuestoadd[1:]]]
            print("Types don't match")
    return newlist

This works for me if there weren't two Type A's in List2 as this causes my code to create two instances of List1. If I was able to add the values at a specific index of the list then that would be great but I can work around that.

It's probably easier to use a dictionary for this:

def merge(d1, d2):
    return {k: v + d2[k] if k in d2 else v for k, v in d1.items()}

d1 = {'A': [1, 2, 3], 'B': [4, 5, 6]}
d2 = {'A': [7, 8, 9], 'C': [0]}
print(merge(d1, d2))

If you must use a list, it's fairly easy to temporarily convert to a dictionary and back to a list:

from collections import defaultdict

def list_to_dict(xss):
    d = defaultdict(list)
    for xs in xss:
    return d

def dict_to_list(d):
    return [[k, *v] for k, v in d.items()]

Import data from another spreadsheet, If you keep data in separate Google Sheets, copy a range of data from one spreadsheet to another with the IMPORTRANGE function. In parenthesis, add the following specifications in quotation marks and separated by a comma: Split data into columns � Space table rows and columns evenly � Share a file publicly� Select the list that you want the new item added to (List B), and then in the area below that select each of the columns that you want updated and the values that should be used (i.e. click Add to add the field and then indicate what the value should be set to).

Rather than using List1 + [valuestoadd[1:]], you should be using newlist[0].append(valuestoadd[1:]) so that it doesn't ever create a new list and only appends to the old one. The [0] is necessary so that it appends to the first sublist rather than the whole list.

newlist = List1 #you're doing this already - might as well initialize the new list with this code
for values in List1:
    for valuestoadd in List2:
        if values[0] == valuestoadd[0]:
            newlist[0].append(valuestoadd[1:]) #adds the values on to the end of the first list
            print("Types don't match")

[['Type A =', 'Value 1', 'Value 2', 'Value 3', ['Value 9', 'Value 10', 'Value 11'], ['Value 12', 'Value 13']], ['Type B =', 'Value 4', 'Value 5']]

This does, sadly, input the values as a list - if you want to split them into individual values, you would need to iterate through the lists you're adding on, and append individual values to newlist[0].

This could be achieved with another for loop, like so:

if values[0] == valuestoadd[0]:
    for subvalues in valuestoadd[1:]: #splits the list into subvalues
        newlist[0].append(subvalues) #appends those subvalues

[['Type A =', 'Value 1', 'Value 2', 'Value 3', 'Value 9', 'Value 10', 'Value 11', 'Value 12', 'Value 13'], ['Type B =', 'Value 4', 'Value 5']]

Dictionaries in Python – Real Python, Python provides another composite data type called a dictionary, which is similar to a list in that it is a Dictionaries and lists share the following characteristics: You can define a dictionary by enclosing a comma-separated list of key-value pairs in curly braces ( {} ). Then you can add new keys and values one at a time :. How to add values in one column based on input in another column I am trying to get the total value of one column based on the data in another column. I want to account for the cells that do not have data currently in them - so they are blank.

I agree with the other answers that it would be better to use a dictionary right away. But if you want, for some reason, stick to the data structure you have, you could transform it into a dictionary and back:

type_dict = {}
for tlist in List1+List2:
    curr_type = tlist[0]
    type_dict[curr_type] = tlist[1:] if not curr_type in type_dict else type_dict[curr_type]+tlist[1:]
new_list = [[k] + type_dict[k] for k in type_dict]

In the creation of new_list, you can take the keys from a subset of type_dict only if you do not want to include all of them.

3. Strings, lists, and tuples — Beginning Python Programming for , In the case of lists or tuples, they are made up of elements, which are values of any The first example is a list of five integers, and the next is a list of three strings. The sequence types share a common set of operations. as the slice operator, but instead of one integer value inside we have two, seperated by a colon ( : ):. public class listClass { public List<Person> People{ get; set; } = new List<Person>(); } In my main form, I have two button AddPerson and AddDate what I'm trying to achieve is to allow (by choosing date from dateTimePicker) add number of dates to one object inside the Person list, i.e one person can have 2-3 dates. What I have tried:

Define attribute lists and ranges—ArcGIS Online Help, Home � Get Started � Create � Analyze � Share � Manage Data � Administer When you define a list of values, you set a code—the value stored in ArcGIS Online—and a label—the value users see in the values list and attribute table when they view the values editors provide when adding or updating an attribute value—for� I need to pull add values from a table which are only associated with a certain criteria in another cell An Excelchat Expert solved this problem in 30 mins! I need a formula to highlight cells or rows in one worksheet containing specific text from another worksheet designated by values in a third column.

Bring! Shopping List, To add an item to your Bring! list, search for it in “What would you like to buy?” add it In the next step you can send out invites to share your list if desired. Also check that on all list participants phones mobile data usage and notifications are� In Excel, you can use formulas to quickly sum the values based on certain criteria in an adjacent column. 1. Copy the column you will sum based on, and then pasted into another column. In our case, we copy the Fruit column and paste in Column E.

Collection Types — The Swift Programming Language (Swift 5.3), This means that you cannot insert a value of the wrong type into a collection by mistake. It An array stores values of the same type in an ordered list. Set b and set c are disjoint with one another, because they share no elements in common. From source worksheet, select the cell that contains data or that you want to link to another worksheet, and copy it by pressing the Copy button from the Home tab or press CTRL+C. Go to the destination worksheet and click the cell where you want to link the cell from the source worksheet.

  • newlist = [List1 + [valuestoadd[1:]] is missing a bracket?
  • Might be easier to do with a dictionary.
  • Well, your base of newlist is always List1, unless the types never match. In that case newlist = []. So your problem is that if List2 has more than 1 list with type A, you don't get the result appended. So why don't you just do newlist = List1. After that if there is a match: newlist.append(valuestoadd[1:]. And if you never appended anything you can simply do newlist = [] in the end. So this suggestion behaves the same as your code and doesn't ignore multiple entries of the same type.
  • @PaulErlenmeyer doing that doesn't work, as it appends to the end of the big list, rather than the type A list. if you observe my answer below, i've laid out what would be required to stick with only lists, ignoring dictionaries.
  • This is clean and pythonic, but here is the output {'A': [7, 8, 9, 1, 2, 3]}, I think he wants B and C too.
  • @Mustapha-Belkacim Thanks, I've now included the 'B', though it doesn't look like he wants 'C' in the example provided.
  • from his question, I think you're right, but it doesn't make sense
  • Thank you. If List2 also had Type B values that I needed to add to the Type B section of List1, would this method work? I think this will work on my code if so.
  • @Dean1886 Yep. Since it looks like you also have duplicate keys within each list, I modified list_to_dict to account for that.
  • Thank you for the response. If List2 also had Type B values that I needed to add to the Type B section of List1, would this method work? I've tried running it but can only get it to add the Type B values to the Type A section of List1.
  • You'd have to change it to newlist[1] because that's where Type B is, so it's likely a dictionary would be much better.
  • Thank you for this
  • Thank you. Think I need to take a further look at dictionaries as I've only really used lists until now.