Skip to content

Instantly share code, notes, and snippets.

@pilinux
Last active April 5, 2017 09:39
Show Gist options
  • Save pilinux/952f9037909b4d790028d7df46544d7f to your computer and use it in GitHub Desktop.
Save pilinux/952f9037909b4d790028d7df46544d7f to your computer and use it in GitHub Desktop.
No Description
##########
# L.append(object) -> – append object to the end of list
# append() takes exactly one argument
L = [1,2,3,4,5]
L.append(2.1)
print(L)
# Output:
# [1, 2, 3, 4, 5, 2.1]
L.append('string')
print(L)
# Output:
# [1, 2, 3, 4, 5, 2.1, 'string']
##########
##########
# L.clear() -> – remove all items from L
# clear() takes no arguments
L = [1,2,3,4,5]
L.clear()
print(L)
# Output:
# []
##########
##########
# L.copy() -> list – a shallow copy of L
# copy() takes no arguments
L = [1,2,3,4,5]
A = L.copy()
print(A)
# Output:
# [1, 2, 3, 4, 5]
A[0] = 6
print(A)
# Output:
# [6, 2, 3, 4, 5]
print(L)
# Output:
# [1, 2, 3, 4, 5]
# Additional Info: Using reference
L = [1,2,3,4,5]
A = L
print(A)
# Output:
# [1, 2, 3, 4, 5]
A[0] = 6
print(A)
# Output:
# [6, 2, 3, 4, 5]
print(L)
# Output:
# [6, 2, 3, 4, 5]
##########
##########
# L.count(value) -> integer – return number of occurrences of value
# count() takes exactly one argument
L = [1, 2, 3, 4, 5, 1, 1, 1, 4, 5]
L.count(1)
# Output:
# 4
##########
##########
# L.extend(iterable) -> – extend list by appending elements from the iterable
# extend() takes exactly one argument
# L.extend(2) - wrong because 'int' object is not iterable
# extend by a list
L = [1,2,3,4,5]
E = [1,1,1,4,5]
L.extend(E) # append the elements of E list to the end of L list
print(L)
# Output:
# [1, 2, 3, 4, 5, 1, 1, 1, 4, 5]
##########
##########
# L.insert(index, object) – insert object before index
# In list, index starts from 0
L = [4, 1, 1, 5, 4, 3, 2, 1]
L.insert(3,7) # insert 7 before index 3 (value of index 3 is 5)
print(L)
# Output:
# [4, 1, 1, 7, 5, 4, 3, 2, 1]
##########
##########
# L.index(value, [start, [stop]]) -> integer – return first index of value
# Raises ValueError if the value is not present
# index() takes at least 1 argument
L = [1,2,1,4,5,2,7,8,9,0]
L.index(1)
# Output:
# 0
L.index(7)
# Output:
# 6
L.index(1,1,9) # find index number of first 1 occurred between index 1 to 9
# Output:
# 2
##########
##########
# L.pop([index]) -> item – remove and return item at index (default last)
# Raises IndexError if list is empty or index is out of range
# default value is -1
L = [1,2,3,4,5,6,7,8,9,0]
L.pop()
# Output:
# 0
print(L)
# Output:
# [1, 2, 3, 4, 5, 6, 7, 8, 9]
L.pop(-1)
# Output:
# 9
print(L)
# Output:
# [1, 2, 3, 4, 5, 6, 7, 8]
L.pop(-5)
# Output:
# 4
print(L)
# Output:
# [1, 2, 3, 5, 6, 7, 8]
L.pop(3)
# Output:
# 5
print(L)
# Output:
# [1, 2, 3, 6, 7, 8]
##########
##########
# L.remove(value) -> – remove first occurrence of value
# Raises ValueError if the value is not present
# remove() takes exactly one argument
L = [5, 4, 1, 1, 1, 5, 4, 3, 2, 1]
L.remove(5)
print(L)
# Output:
# [4, 1, 1, 1, 5, 4, 3, 2, 1]
##########
##########
# L.reverse() – reverse
# reverse() takes no arguments
L = [1, 2, 3, 4, 5, 1, 1, 1, 4, 5]
L.reverse()
print(L)
# Output:
# [5, 4, 1, 1, 1, 5, 4, 3, 2, 1]
##########
##########
# L.sort(key=keyword, reverse=False(default)/True) -> – sort elements
L = [1, 2, 3, 4, 5, 1, 1, 1, 4, 5]
L.sort()
print(L)
# Output
# [1, 1, 1, 1, 2, 3, 4, 4, 5, 5]
L = [1, 2, 3, 4, 5, 1, 1, 1, 4, 5]
L.sort(reverse=True)
print(L)
# Output
# [5, 5, 4, 4, 3, 2, 1, 1, 1, 1]
##########
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment