0

Instruction: The get_name_list() function returns a list of all the names in the parameter: name_list, with a given letter provided in parameter: to_look_for.

Question: If I remove the square brackets of 'name' in 'a_list += [name]' expression, it will show the following wrong output. However, if the square brackets or 'append()' method is used, the right output will be produced (2nd correct output below). I wonder why without the square brackets in [name], the right output is not produced?

Wrong output:

names with d ['J', 'a', 'd', 'e']
names with k ['M', 'i', 'k', 'e', 'y']

Wrong code:

def main():
    names = ["Jasper", "Jade", "Mikey", "Giani"]
    names_d = get_name_list(names, "d")
    names_k = get_name_list(names, "k")
    print("names with d", names_d)
    print("names with k", names_k)

    def get_name_list(name_list, to_look_for):

        a_list = [] 
        for name in name_list:
            #print(name)
            if to_look_for in name:
                print(name)
                a_list += name
                #a_list.append(name)

        return a_list

    main()

Correct output:

names with d ['Jade']
names with k ['Mikey']

Correct code:

def main():
    names = ["Jasper", "Jade", "Mikey", "Giani"]
    names_d = get_name_list(names, "d")
    names_k = get_name_list(names, "k")
    print("names with d", names_d)
    print("names with k", names_k)    

def get_name_list(name_list, to_look_for):

    a_list = [] 
    for name in name_list:
        #print(name)
        if to_look_for in name:
            print(name)
            #a_list += name
            a_list.append(name)

    return a_list

main()

1 Answer 1

3

'+=' operation is equal to extend() of list built-in method. It will change value right to equal-sign to a list first and then do extending.

In your case:

a_list += name  # which is the same as a_list.extend(name), although name is a string, it will be converted to a list first

When a string is converted to a list, it will act like changing 'abc' to ['a', 'b', 'c']. That's where your ['J', 'a', 'd', 'e'] comes from

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thank you for your answer. I kind of got your idea: When a string is converted to a list, it will act like changing 'abc' to ['a', 'b', 'c']. One question: The 'name' is an element of the list: 'name_list', therefore, 'name' should be a list not a string. I wonder why 'name' is a string?
@jmk 'for name in name_list' means you assign every element in name_list to variable name. And each element in name_list is a string. You can check with type(name_list[0]). If you want to get first element as a list, for instance, you should use name_list[0:1].

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.