Star expressions

Page content
  • They are useful when iterating over a sequence of tuples of varying lengths.

  • Also, they are useful when combined with certain kinds of string processing operations, such as splitting.

    line = 'nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false'
    
    uname, *fields, homedir, sh = line.split(':')
    
  • Examples

    • You want to drop the first and last grades and return the average of the remaining grades:

      def drop_first_last(grades):
        first, *middle, last = grades
        return avg(middle)
      
    • You have a single name and email, but an arbitrary number of phone numbers.

      name, email, *phone_numbers = ('Dave', '[email protected]', '2312', '32121')
      # phone_numbers = ['2312', '32121']
      

      Here phone_numbers will be always a list, regardless of the number of numbers including None.

    • name, *_, (*_, year) = ('ACME', 50, 123.45, (12, 18, 2012))
      # name = 'ACME', year=2012