hamburger menu
All Coursesall course arrow
adda247
reward-icon
adda247
    arrow
    arrow
    arrow
    Given the following assignment of a variable LS = [1, 9, 2, 8, 3, 7, 5, 4] What will be the output of the following? print(LS[: :2] + LS[: :-
    Question

    Given the following assignment of a variable
    LS = [1, 9, 2, 8, 3, 7, 5, 4]
    What will be the output of the following?
    print(LS[: :2] + LS[: :-2])

    A.

    [5, 3, 2, 1, 9, 8, 7, 4]

    B.

    [1, 2, 3, 5, 4, 7, 8, 9]

    C.

    [4, 7, 8, 9, 1, 2, 3, 5]

    D.

    [9, 8, 7, 4, 5, 3, 2, 1]

    Correct option is D


    The list LS is defined as:
    LS = [1, 9, 2, 8, 3, 7, 5, 4]
    The code LS[: :2] + LS[: :-2] performs the following operations:
    1. LS[: :2]: This takes every second element from the list starting from index 0 (i.e., it selects every alternate element):
    · The result is [1, 2, 3, 5] because we are picking every second element starting from the first element.
    2. LS[: :-2]: This takes every second element from the list, but in reverse order (since the step is -2):
    · The result is [4, 7, 8, 9] because we start from the last element (4) and move backward, picking every second element in reverse order.
    When we concatenate these two slices (LS[: :2] + LS[: :-2]), the result is:
    [1, 2, 3, 5] + [4, 7, 8, 9]
    So, the final result is:
    [1, 2, 3, 5, 4, 7, 8, 9]
    Important Key Points:
    1. Slicing: The [: :2] slice selects elements with a step of 2, starting from the first element.
    2. Reverse Slicing: The [: :-2] slice selects elements with a step of -2, starting from the last element and moving backward.
    3. Concatenation: The two slices are concatenated using the + operator, resulting in the combined list.
    Knowledge Booster:
    · Option (a): This is incorrect. The result does not match the expected output for either of the slices.
    · Option (b): This is incorrect. The result of the concatenation doesn't match the expected sequence.
    · Option (c): This is incorrect. The slices do not match the expected output.

    Free Tests

    Free
    Must Attempt

    CDP Subject Test 01

    languageIcon English
    • pdpQsnIcon20 Questions
    • pdpsheetsIcon20 Marks
    • timerIcon20 Mins
    languageIcon English
    Free
    Must Attempt

    EVS Subject Test 01

    languageIcon English
    • pdpQsnIcon20 Questions
    • pdpsheetsIcon20 Marks
    • timerIcon20 Mins
    languageIcon English
    Free
    Must Attempt

    CTET Paper-I PYP (07 July 2024)

    languageIcon English
    • pdpQsnIcon150 Questions
    • pdpsheetsIcon150 Marks
    • timerIcon150 Mins
    languageIcon English
    test-prime-package

    Access ‘EMRS PGT’ Mock Tests with

    • 60000+ Mocks and Previous Year Papers
    • Unlimited Re-Attempts
    • Personalised Report Card
    • 500% Refund on Final Selection
    • Largest Community
    students-icon
    289k+ students have already unlocked exclusive benefits with Test Prime!
    Our Plans
    Monthsup-arrow