Quick actions

cmd+k|ctrl+k

Navigation

Languages

List Slicing

Snippet info

Language

Python

Visibility

public

Author

narutouzumakiramen18

Created

2026-05-03T06:22:42.633199Z

Updated

2026-05-03T06:26:05.058232Z

# list slicing 
amazon_cart = [
    'notebooks',
    'consoles',
    'sunglasses',
    'toys',
    'grapes'
    ]
    
print(amazon_cart[::2])
# In string we have noticed we cannot change the objs via indexing.
# But here we can do it here!
# Strings are immutable while lists are mutable.

amazon_cart[0] = 'Laptop'
print(amazon_cart) #Notebook is replaced by laptop!

# Everytime we do a list slicing we create a new copy of the list in the memory to prove this
sale = amazon_cart[0:3]
print()
# we now have 2 copies of the same list.
print(sale)
print(amazon_cart)
INFO