From 15a7a7e126a1e969dd31c6fc13a361aea80c9165 Mon Sep 17 00:00:00 2001 From: Michael Hienle <40914576+mhienle@users.noreply.github.com> Date: Fri, 20 Jul 2018 16:40:38 +0200 Subject: [PATCH] Minor improvements Just made some things clearer by comments. --- python.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/python.md b/python.md index eaa1b92d6..71115659f 100644 --- a/python.md +++ b/python.md @@ -6,17 +6,17 @@ title: Python list = [] list[i:j] # returns list subset - list[-1] # access last element - list[:-1] # return all but the last element + list[-1] # returns last element + list[:-1] # returns all but the last element list[i] = val list[i:j] = otherlist # replace ith to jth element with otherlist del list[i:j] list.append(item) - list.extend(list) + list.extend(another_list) list.insert(0, item) - list.pop() + list.pop() # returns and removes last element from the list list.remove(i) list1 + list2 # combine two list set(list) # remove duplicate elements from a list @@ -25,19 +25,19 @@ title: Python list.count(item) sum(list) - list.sort() + list.sort() # sorts in-place, returns None zip(list1, list2) - sorted(list) - ",".join(list) + sorted(list) # returns sorted copy of list + ",".join(list) # returns a string with list elements seperated by comma ### Dict dict.keys() dict.values() - "key" in dict - dict["key"] # throws KeyError - dict.get("key") + "key" in dict # let's say this returns False, then... + dict["key"] # ...this raises KeyError + dict.get("key") # ...this returns None dict.setdefault("key", 1) ### Iteration