1

I am trying to pass class element to method. Element is formed dynamically inserting current time in it. Mine class looks something like this:

class MineContact(dict):
    def __init__(self, **kwargs):
        # set your default values
        import time
        curr_time = repr(time.time()).replace('.', '') 
        self['givenName'] = ['name%s' % curr_time[10:]]
        ...

So, I create object of this class and now I want to insert it as method argument:

    contact = MineContact()
    extra_text = "-%d" % (self.iteration)
    new_contact.insert_given_name(contact.givenName + extra_text)

When I run this script, I get this type of error:

TypeError: can only concatenate list (not "str") to list

So, does anyone knows where am I getting it wrong?

0

2 Answers 2

3

givenName seems to be a list. You can append another list like this:

new_contact.insert_given_name(contact.givenName + [extra_text])
Sign up to request clarification or add additional context in comments.

Comments

-1

contact.givenName is a list, and extra_text is a string. in python you can add string to string, or list to list. you can't add string to list.

if you'd like to add string to a list, use list.append method.

mylist.append(mystr)

4 Comments

That modifies the existing list which is not what the OP is trying to do.
Also, that will pass None when passed as a function parameter since list.append(val) returns None
I am telling him how to concatate list and string. why you think I give him code to copy and paste?
Your approach is fundamentally wrong - the code will not work in his use case. The .append() method can be used to append a string to an existing list, not to create a new list from a given list, extended by one element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.