GreenSalamanderCS's post

List Splicing in Python

Here is an example of how list splicing works in Python.

 

    # list to be spliced
    my_list = [1, 2, 3, 4, 5]

    # splicing the list
    new_list = my_list[2:4]

    # printing the result
    print(new_list) # Output: [3, 4]

 

In the above code block, we start with a list that has five elements, then we create a new list which is based on the first list, but with only elements at index 2 & 3 in it, which results in the above output.

More from GreenSalamanderCS