w3resource

Python: Extend a list without append


Extend List Without Append

Write a Python program to extend a list without appending.

Visual Presentation:

Python List: Extend a list without append.

Sample Solution:

Python Code:

# Define two lists, 'x' and 'y', containing integers
x = [10, 20, 30]
y = [40, 50, 60]

# Use list slicing to insert the elements of 'y' at the beginning of 'x' by setting 'x[:0] = y'
x[:0] =y
# This effectively adds the elements of 'y' to the front of 'x'
print(x)

Sample Output:

[40, 50, 60, 10, 20, 30] 

Flowchart:

Flowchart: Extend a list without append

Python Code Editor:

Previous: Write a Python program to find all the values in a list are greater than a specified number.
Next: Write a Python program to remove duplicates from a list of lists.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.