Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python tricks

# Python's list slice syntax can be used without indices
# for a few fun and useful things:

# You can clear all elements from a list:
list = [1, 2, 3, 4, 5]
del lst[:]

print(list)

# Output
# []

# You can replace all elements of a list
# without creating a new list object:
a = list
lst[:] = [7, 8, 9]
print(list)

# Output
# [7, 8, 9]

print(a)

# Output
# [7, 8, 9]

print(a is list)

# Output
# True

# You can also create a (shallow) copy of a list:
b = list[:]
print(b)

# Output
# [7, 8, 9]

print(b is lst)

# Output
# False
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #python #tricks
ADD COMMENT
Topic
Name
7+2 =