2

Would someone be able to help me understanding how can I pass values from multiple lists as function parameters? I'm trying to update email for each of myemailId with url that includes customerId.

my code so far:

emailId = [732853380,7331635674]
customerId = ['cust-12345-mer','cust-6789-mer']

for x, y in zip(emailId, customerId):
    def update_email(emailId, token, user, notes="https://myurl.com/customer?customerId =" + customerId):
        headers = {     'accept': 'application/json',
                    'Content-Type': 'application/json',
                    'token': token,
                    'user': user}
        endpoint = 'email/'
        body = {'emailId': emailId, 'user': user, 'notes': notes}
        requests.put(url = host + endpoint, headers = headers, json=body)
        return True

but receiving this error that is corresponding to the line that starts with def update_email...:

TypeError: must be str, not list

Thanks in advance!

Gabio
  • 9,126
  • 3
  • 12
  • 32
Baobab1988
  • 685
  • 13
  • 33

2 Answers2

4

First of all, you shouldn't define the function for each loop iteration but once before executing the loop.

In order to pass the values, use:

emailId = [732853380, 7331635674]
customerId = ['cust-12345-mer', 'cust-6789-mer']


def update_email(emailId, token, user, customerId):
    notes = "https://myurl.com/customer?customerId =" + customerId
    headers = {'accept': 'application/json',
               'Content-Type': 'application/json',
               'token': token,
               'user': user}
    endpoint = 'email/'
    body = {'emailId': emailId, 'user': user, 'notes': notes}
    requests.put(url=host + endpoint, headers=headers, json=body)
    return True


for x, y in zip(emailId, customerId):
    update_email(x, token, user, y)

Gabio
  • 9,126
  • 3
  • 12
  • 32
2

customerId is the list, y is the value from that list. Use

notes="https://myurl.com/customer?customerId =" + y
Alex Hall
  • 34,833
  • 5
  • 57
  • 89
  • @Gabip right, and I've asked them what on Earth they're doing. But I also think it's useful to understand what caused the error. – Alex Hall Apr 21 '20 at 08:21
  • Agree, I saw your comment afterwards, that's why I removed my comment... – Gabio Apr 21 '20 at 08:22