concept list.append in category python

appears as: list.append
Tiny Python Projects: Learn coding and testing with puzzles and games

This is an excerpt from Manning's book Tiny Python Projects: Learn coding and testing with puzzles and games.

  • There are methods like list.append() and list.extend() to add elements to a list. Use list.pop() and list.remove() to remove elements.
  • 12.3.3 Another way to write list.append()

    The solution in section 12.2 creates an empty list, to which I list.append() the return from choose(). Another way to write list.append() is to use the += operator to add the right-hand value (the element to add) to the left-hand side (the list), as in figure 12.3.

    def main():
        args = get_args()
        random.seed(args.seed)
     
        ransom = []
        for char in args.text:
            ransom += choose(char)
     
        print(''.join(ransom))
    Figure 12.3 The += operator is another way to write list.append().