6. Simple Collections

6.1. list

  • Mutable - can add, remove, and modify values

  • Defining with list() is more readable, but [] is used more often:

    my_list = []
    my_list = list()
    
  • Brackets are required

  • No need for comma for one element list:

    my_list = [1]
    
  • list can store elements of any types:

    my_list = [1, 2.0, None, False, 'José']
    
  • You can also add list to list:

    my_list = [1, 2.0, None, False, 'José', [1, 'hello']]
    
  • Slicing, the same as for strings:

    my_list = [1, 2.0, None, False, 'José']
    
    my_list[1]             # 2.0
    my_list[2:4]           # [None, False]
    my_list[::2]           # [1, None, 'José']
    my_list[-1]            # 'José'
    

6.1.1. Adding list to list

my_list = [1, 2]

my_list + [3, 4]        # [1, 2, 3, 4]

6.1.2. Appending elements

my_list = [1, 2]

my_list.append(3)       # [1, 2, 3]
my_list.append([4, 5])  # [1, 2, 3, [4, 5]]

6.1.3. Extending lists

my_list = [1, 2]

my_list.extend([3, 4])  # [1, 2, 3, 4]

6.1.4. Inserting elements at specific position

my_list = [1, 2]

my_list.insert(0, 'a')  # ['a', 1, 2]

6.1.5. Length of a list

len([1, 2, 3])  # 3
len([])         # 0

6.2. set

  • Defining only with set():

    my_set = set()
    
  • No need for comma for one element set:

    my_set = {1}
    
  • Only unique values:

    my_set = {1, 3, 1}          # {1, 3}
    
  • Can store any hashable elements:

    my_set = {1, 2.0, 'Jose'}   # {1, 2.0, 'Jose'}
    my_set = {1, 2.0, [3, 4]}   # TypeError: unhashable type: 'list'
    my_set = {1, 2.0, {3, 4}}   # TypeError: unhashable type: 'set'
    
  • Mutable - can add, remove, and modify values:

    my_set = {1, 2, 3}          # {1, 2, 3}
    
    my_set.add(4)               # {1, 2, 3, 4}
    my_set.add(4)               # {1, 2, 3, 4}
    my_set.add(3)               # {1, 2, 3, 4}
    
    my_set.update([4, 5])       # {1, 2, 3, 4, 5}
    my_set.update({4, 5})       # {1, 2, 3, 4, 5}
    
  • Use of set operations with special syntax:

    {1,2} - {2,3}               # {1}        # Subtract
    {1,2} | {2,3}               # {1, 2, 3}  # Sum
    {1,2} & {2,3}               # {2}        # Union
    {1,2} ^ {2,3}               # {1, 3}     # Symmetrical difference
    {1,2} + {3,4}               # TypeError: unsupported operand type(s) for +: 'set' and 'set'
    
  • Slicing set is not possible:

    my_set = {1, 2.0, None, False, 'José'}
    
    my_set[1]                   # TypeError: 'set' object does not support indexing
    my_set[2:4]                 # TypeError: 'set' object does not support indexing
    
  • Length of a set:

    my_set = {1, 2, 3}
    len(my_set)                 # 3
    
  • Converting list to set deduplicate items:

    names = ['Matt', 'Иван', 'José', 'Matt']
    
    unique_names = set(names)
    # {'Matt', 'Иван', 'José'}
    

6.3. tuple

  • Immutable - cannot add, modify or remove elements

  • Defining with tuple() is more readable, but () is used more often:

    my_tuple = ()
    my_tuple = tuple()
    
  • Single element tuple require comma at the end (important!)

  • Braces are optional:

    my_tuple = 1,
    my_tuple = (1,)
    
  • Can store any type:

    my_tuple = 1, 2.0, None, False, 'José'
    my_tuple = (1, 2.0, None, False, 'José')
    
  • Slicing tuple:

    my_tuple = (1, 2, 3, 4, 5)
    
    my_tuple[2]             # 3
    my_tuple[-1]            # 5
    my_tuple[:3]            # (1, 2, 3)
    my_tuple[3:]            # (4, 5)
    my_tuple[::2]           # (1, 3, 5)
    my_tuple[1:4]           # (2, 3, 4)
    
    my_tuple = (1, 2, 3, 4, 5)
    
    MIN = 1
    MAX = 4
    my_tuple[MIN:MAX]       # (2, 3, 4)
    
    my_tuple = (1, 2, 3, 4, 5)
    
    BETWEEN = slice(1, 4)
    my_tuple[BETWEEN]       # (2, 3, 4)
    
  • Length of a tuple:

    my_tuple = (1, 2, 3, 4, 5)
    len(my_tuple)           # 5
    

6.4. Assigning sequences values to variables

a, b, c = 1, 2, 3
a, b, c = (1, 2, 3)
a, b, c = [1, 2, 3]
a, b, c = {1, 2, 3}

6.5. How Python understands types?

  • Result of a type(what) for each line:

    what = 1.2        # <class 'float'>
    what = 1,2        # <class 'tuple'>
    
    what = 1, 2       # <class 'tuple'>
    what = (1, 2)     # <class 'tuple'>
    
    what = (1,2)      # <class 'tuple'>
    what = (1.2)      # <class 'float'>
    what = (1.2,)     # <class 'tuple'>
    
    what = 'foo'      # <class 'str'>
    what = ('foo')    # <class 'str'>
    
    what = 'foo',     # <class 'tuple'>
    what = ('foo',)   # <class 'tuple'>
    
    what = 1.        # <class 'float'>
    what = (1.)      # <class 'float'>
    
    what = .5        # <class 'float'>
    what = (.5)      # <class 'float'>
    
    what = 1.0       # <class 'float'>
    what = 1         # <class 'int'>
    
    what = 10.5      # <class 'float'>
    what = (10.5)    # <class 'float'>
    
    what = 10,5      # <class 'tuple'>
    what = (10,5)    # <class 'tuple'>
    
    what = 10.       # <class 'float'>
    what = (10.)     # <class 'float'>
    
    what = 10,       # <class 'tuple'>
    what = (10,)     # <class 'tuple'>
    
    what = 10        # <class 'int'>
    what = (10)      # <class 'int'>
    
    what = (1.,1.)   # <class 'tuple'>
    what = (.5,.5)   # <class 'tuple'>
    what = (1.,.5)   # <class 'tuple'>
    
    what = 1.,.5     # <class 'tuple'>
    

6.6. More advanced topics

Note

The topic will be continued in Intermediate and Advanced part of the book

6.7. Assignments

6.7.1. Simple collections

  1. Stwórz tuple z cyframi od 0-9
  2. Przekonwertuj ją do list
  3. Na pierwsze miejsce w liście dodaj całą oryginalną tuple
  4. Przekonwertuj wszystko na płaski set unikalnych wartości wykorzystując slice
About:
  • Filename: sequences_conversions.py
  • Lines of code to write: 5 lines
  • Estimated time of completion: 10 min
The whys and wherefores:
 
  • Definiowanie i korzystanie z list, tuple, set
  • Rzutowanie i konwersja typów