Tagged: Python
7 articles tagged Python across all clusters on CodeWalkers.
Python List insert(): Add an Item at Any Position
The insert() method puts one item into a list before a given index. Syntax, how negative and out-of-range indexes behave, the TypeErrors it raises, and when append(), slice assignment or deque fit better.
Python List append(): Add One Item to the End of a List
The append() method adds one item to the end of a list and returns None. Syntax, what happens when you append a list, the AttributeError and TypeError it leads to, and how it compares with extend() and +=.
Python List remove() vs pop(): Delete by Value or by Index
The remove() method deletes the first matching value and pop() deletes by position and returns the item. Syntax, return values, the ValueError and IndexError each raises, and how both compare with del and clear().
Python List and Dict Comprehensions: Syntax and Examples
A comprehension builds a list or a dictionary from an existing iterable in one expression. The syntax for both forms, filtering, conditional values, nesting, and the cases where a plain loop is the better answer.
Lists vs Dictionaries vs Sets in Python
Three built-in collections answer three different questions: what came in what order, what is stored under this name, and have I seen this before. A criteria-based way to pick one, with the cost of picking wrong.
Python Dictionaries: Keys, Values, Updates, Loops, and JSON
A Python dictionary stores values under names you choose instead of numbered positions. Creating one, reading keys safely, updating and merging, looping over views, counting and grouping, and the JSON boundary.
Python Lists: Create, Slice, and Iterate
A Python list keeps items in the order you put them and lets you change that order later. How to create one, index and slice it, add and remove items, copy it safely, and recognize the jobs a list is wrong for.