commit
e9ac8eb8f4
20
python.md
20
python.md
|
@ -6,17 +6,17 @@ title: Python
|
||||||
|
|
||||||
list = []
|
list = []
|
||||||
list[i:j] # returns list subset
|
list[i:j] # returns list subset
|
||||||
list[-1] # access last element
|
list[-1] # returns last element
|
||||||
list[:-1] # return all but the last element
|
list[:-1] # returns all but the last element
|
||||||
|
|
||||||
list[i] = val
|
list[i] = val
|
||||||
list[i:j] = otherlist # replace ith to jth element with otherlist
|
list[i:j] = otherlist # replace ith to jth element with otherlist
|
||||||
del list[i:j]
|
del list[i:j]
|
||||||
|
|
||||||
list.append(item)
|
list.append(item)
|
||||||
list.extend(list)
|
list.extend(another_list)
|
||||||
list.insert(0, item)
|
list.insert(0, item)
|
||||||
list.pop()
|
list.pop() # returns and removes last element from the list
|
||||||
list.remove(i)
|
list.remove(i)
|
||||||
list1 + list2 # combine two list
|
list1 + list2 # combine two list
|
||||||
set(list) # remove duplicate elements from a list
|
set(list) # remove duplicate elements from a list
|
||||||
|
@ -25,19 +25,19 @@ title: Python
|
||||||
list.count(item)
|
list.count(item)
|
||||||
sum(list)
|
sum(list)
|
||||||
|
|
||||||
list.sort()
|
list.sort() # sorts in-place, returns None
|
||||||
|
|
||||||
zip(list1, list2)
|
zip(list1, list2)
|
||||||
sorted(list)
|
sorted(list) # returns sorted copy of list
|
||||||
",".join(list)
|
",".join(list) # returns a string with list elements seperated by comma
|
||||||
|
|
||||||
### Dict
|
### Dict
|
||||||
|
|
||||||
dict.keys()
|
dict.keys()
|
||||||
dict.values()
|
dict.values()
|
||||||
"key" in dict
|
"key" in dict # let's say this returns False, then...
|
||||||
dict["key"] # throws KeyError
|
dict["key"] # ...this raises KeyError
|
||||||
dict.get("key")
|
dict.get("key") # ...this returns None
|
||||||
dict.setdefault("key", 1)
|
dict.setdefault("key", 1)
|
||||||
|
|
||||||
### Iteration
|
### Iteration
|
||||||
|
|
Loading…
Reference in New Issue