List data structure


  • Python list, is one of the "compound" data types.

    • Meaning, it groups similar or dissimilar data objects.

    • Also can be called collection of data objects.

  • Python list, can contain any data type.

    • More advanced data types too like 'list' themselves.

    • These can be thought of as collection of 'sub-lists'.

  • List, can contain dissimilar data objects as well, as formerly mentioned.

    • So we can have, floats, int's, boolean, list.
  • Python list are represented as:

    variable = [ element1, element2, element3, ... , element(n-1) ]


Exercise: Review Questions :-


  • RQ1 : Which of the following is a characteristic of a Python list?

    Ans: It is a way to name a collection of values, instead of having to create seperate variables for each element.

  • RQ2 : You use type(fam) and type(fam2) to reveal the type of a Python list. What is the result?

    Ans: list.

  • RQ3 : Which comand is "invalid" Python syntax to create a list x?.

    Ans: x = [ "this", "is", "a" True, "list" ]

    Note: x = [ "this" + "is", "a", True, "list" ] is a valid syntax.

  • RQ4 :Which three Python data typesdoes this list contain?

    x = [ "you", 2, "are", "so", True ]

    Ans: int, string and bool.

Lab: Python Lists


Objective:

  • Creating Python lists in different ways.
  • Discovering interestign aspects about them.

1. Creating a list - 100xp, Status: Earned


In [1]:
"""

After measuring the height of your family, you decide to collect some information
on the house you're living in. The areas of the different parts of your house
are stored in separate variables for now, as shown in the script.

Instructions:

    + Create a list, "areas", that contain the area of:
        
        - hallway(hall), kitchen (kit), living room(liv), bedroom(bed) and bathroom(bath).
        
        - Preserve the order.
        
    + Print "areas" with the "print()" function.
    
"""

# area variables (in square meters)
hall = 11.25
kit = 18.0
liv = 20.0
bed = 10.75
bath = 9.50

# Create list areas
areas = [ hall, kit, liv, bed, bath ]

# Print areas
print( areas )


[11.25, 18.0, 20.0, 10.75, 9.5]

2. Creating list with different types - 100xp, Status : Earned


  • A list can contain any Python type and a mix of Python types as well.

    variable = [ <int>, <float>, <str>, <list> ]



In [4]:
"""
Problem summary:

   The code on the right is the start of a solution. For some of the areas, 
   the name of the corresponding room is already placed in front. Pay attention here! 
   "bathroom" is a string, while bath is a variable that represents the float 9.50 you specified earlier.
   
Instruction:

    + Create the "areas" list such that, 
    
        - List first contains the name of each room as a string,
          and then its area.
        
        - More specifically, add the strings "hallway", "kitchen" and "bedroom"
          at the appropriate locations.
          
    + Print "areas" again; is the printout more informative this time?


"""

# area variables (in square meters)
hall = 11.25
kit = 18.0
liv = 20.0
bed = 10.75
bath = 9.50

# Adapt list areas
areas = ["hallway", hall,"kitchen", kit, "living room", liv, "bedroom", bed, "bathroom", bath]

# Print areas
print(areas)


['hallway', 11.25, 'kitchen', 18.0, 'living room', 20.0, 'bedroom', 10.75, 'bathroom', 9.5]

3. Select the valid list -- 50xp, status : Earned


  • List is also a Python type.

    • Meaning, list can selfcontain a list! Or a sub-list.
  • Syntax : variable_name = [ elem1, elem2, elem3 ... ,elem(n -1) ]

Can you tell which ones of the following lines of Python code are valid ways to build a list?_

A) `[ 1, 3, 4, 2 ]`

B) `[ [ 1, 2, 3 ], [ 4, 5, 7 ] ]`

C) `[ 1 + 2, "a" * 5, 3 ]`

Ans: All of them.


In [5]:
list1 = [ [ 1, 2, 3 ], [ 4, 5, 7 ] ]

list2 = [ 1 + 2, "a" * 5, 3 ]

# print the list to check any errors

print( list1 )

print( list2 )


[[1, 2, 3], [4, 5, 7]]
[3, 'aaaaa', 3]

4 List of Lists -- 100xp, status: Earned


In [7]:
"""
Problem summary:

    As a data scientist, you'll often be dealing with a lot of data,
    and it will make sense to group some of this data.

    Instead of creating a flat list containing strings and floats,
    representing the names and areas of the rooms in your house,
    you can create a list of lists. The script on the right can already give you an idea. 

    Don't get confused here: "hallway" is a string,
    while hall is a variable that represents the float 11.25 you specified earlier.


Instructions:

    + Finish the list of lists so that it also contains the bedroom and bathroom data.
      
        - Make sure you enter these in order! 
    
    + Print out "house";
    
        - Dows this way of structuring your data make more sense?
        
    + Printt out the type of "house".
    
        - Are you still dealing with a list?

"""

# area variables (in square meters)
hall = 11.25
kit = 18.0
liv = 20.0
bed = 10.75
bath = 9.50

# house information as list of lists
house = [ ["hallway", hall],
          ["kitchen", kit],
          ["living room", liv],
          ["bedroom", bed],
          ["bathroom", bath] ]

# Print out house
print( house )

# Print out the type of house
print( type( house ) )


[['hallway', 11.25], ['kitchen', 18.0], ['living room', 20.0], ['bedroom', 10.75], ['bathroom', 9.5]]
<class 'list'>


In [ ]: