If we have two python tuples list as follows: List1=[10,20,30]…

2023

If we have two python tuples list as follows: List1=[10,20,30] List2=[40,50,60] If we want to generate a list List3 as List3=[10,20,30,40,50,60] then the best python command out of the following is :

  1. A.

    List3=List1 + List2 / List3=List1 + List2

  2. B.

    List3= List1.append (List2) / List3= List1.append (List2)

  3. C.

    List3= List1.update (List2) / List3= List1.update (List2)

  4. D.

    List3= List1.extend (List2) / List3= List1.extend (List2)

Attempted by 1798 students.

Show answer & explanation

Correct answer: A

Answer: Use List3 = List1 + List2 to concatenate the two lists.

Why this is correct:

  • The + operator concatenates two lists and returns a new list. For example, List3 = List1 + List2 results in [10, 20, 30, 40, 50, 60].

  • append adds its argument as a single element to the end of the list and returns None. So List1.append(List2) makes List1 = [10, 20, 30, [40, 50, 60]] and assigning its result (List3 = List1.append(List2)) sets List3 to None.

  • extend adds each element from the second list into the first list in-place and returns None. Doing List3 = List1.extend(List2) will make List3 None. To use extend without losing a separate result, copy first: List3 = List1.copy(); List3.extend(List2).

  • update is not a list method; it belongs to dicts or sets, so it cannot be used to combine lists.

Recommended usage example:

List1 = [10, 20, 30]

List2 = [40, 50, 60]

List3 = List1 + List2 # Result: [10, 20, 30, 40, 50, 60]

Explore the full course: Rssb Senior Computer Instructor