# 5.4 Unpacking Sequences¶

- Can unpack any sequence’s elements by assigning the sequence to a comma-separated list of variables (of the appropriate length).

In [1]:

    student_tuple = ('Amanda', [98, 85, 87])

In [2]:

    first_name, grades = student_tuple

In [3]:

    first_name

Out[3]:

    'Amanda'

In [4]:

    grades

Out[4]:

    [98, 85, 87]

In [5]:

    first, second = 'hi'

In [6]:

    print(f'{first}  {second}')

    h  i

In [7]:

    number1, number2, number3 = [2, 3, 5]

In [8]:

    print(f'{number1}  {number2}  {number3}')

    2  3  5

In [9]:

    number1, number2, number3 = range(10, 40, 10)

In [10]:

    print(f'{number1}  {number2}  {number3}')

    10  20  30

### Swapping Values Via Packing and Unpacking¶

In [11]:

    number1 = 99

In [12]:

    number2 = 22

In [13]:

    number1, number2 = (number2, number1)

In [14]:

    print(f'number1 = {number1}; number2 = {number2}')

    number1 = 22; number2 = 99

### Accessing Indices and Values Safely with Built-in Function enumerate¶

- Preferred way to access an element’s index and value is the built-in function `enumerate`.
- Receives an iterable and creates an iterator that, for each element, returns a tuple containing the element’s index and value.
- Built-in function `list` creates a list from a sequence.

In [15]:

    colors = ['red', 'orange', 'yellow']

In [16]:

    list(enumerate(colors))

Out[16]:

    [(0, 'red'), (1, 'orange'), (2, 'yellow')]

- Built-in function `tuple` creates a tuple from a sequence.

In [17]:

    tuple(enumerate(colors))

Out[17]:

    ((0, 'red'), (1, 'orange'), (2, 'yellow'))

In [18]:

    for index, value in enumerate(colors):
        print(f'{index}: {value}')

    0: red
    1: orange
    2: yellow

### Creating a Primitive Bar Chart¶

    # fig05_01.py
    """Displaying a bar chart"""
    numbers = [19, 3, 15, 7, 11]

    print('\nCreating a bar chart from numbers:')
    print(f'Index{"Value":>8}   Bar')

    for index, value in enumerate(numbers):
        print(f'{index:>5}{value:>8}   {"*" * value}')

In [19]:

    run fig05_01.py

    Creating a bar chart from numbers:
    Index   Value   Bar
        0      19   *******************
        1       3   ***
        2      15   ***************
        3       7   *******
        4      11   ***********

- The expression `python "*" * value` creates a string consisting of `value` asterisks.
- When used with a sequence, the multiplication operator (`*`) repeats the sequence.

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.