4 ms·
This cheat sheet sucks. Some examples: > list1.extend(object1): Extends list1 with object1 creating a whole list instead of append which jams a an object insid
by nemetroid 14y ago
This cheat sheet sucks. Some examples:
> list1.extend(object1): Extends list1 with object1 creating a whole list instead of append which jams a an object inside the list.
Extend takes an iterable and not an object. Not sure I can even comprehend that sentence.
> dictionary1.update(dictionary2): updates dictionary1 with values from identical keys in dictionary2
Identical to what? Update adds all key-value pairs from dictionary2. I guess the text might refer to the fact that update overwrites existing values.
> ‘string1’.strip(): Returns one string with whitespace removed.
Only at the start and end of the string.
> ’string1’.count(object1): Returns the number of times that object1 appears in string1.
This method doesn't exist as far as I can tell and I'm not sure how an object "appears" in a string.
- TazeTSchnitzel 14y agoLooks like it exists, and works (at least for strings): >>> 'test'.count('e') 1 >>> 'test'.count('') 5 >>> 'test'.count('tes') 1 >>> 'test'.count('t') 2 >>> 'test'.count('apple') 0 Seems to be the equivalent of `in` on a string but with a count.
- nemetroid 14y agoIt does exist for strings, but the list contains both `.count('string2')` and `.count(object1)`.
- TazeTSchnitzel 14y agoIt also works on lists, I think he got confused.
- rmc 14y agoAren't all iterables objects? You can turn any object into an iterable by adding through __iter__ method IIRC
- clinth 14y agoYes, but all objects are not iterables. If you list1.extend(object1) as the cheat sheet describes and object1 has no __iter__, it won't work. Likewise, you can iterate strings, so understanding the target of the iterable will be frustrating for list_of_strings.extend('newstring'). This is better described in python's native help documentation: $ python >>> help() >>> list searching for extend via '/extend': | extend(...) | L.extend(iterable) -- extend list by appending elements from the iterable Concise and correct.
- mercuryrising 14y ago> This cheat sheet sucks. Some examples: Make a better one and get it to the front page of HN.
- nemetroid 14y agoWhether or not I make one has no bearing on this one's quality.