Manipulating Lists


Python lists can manuipulated as follows:

  • Change List elements.
  • Add list elements.
  • Remove list elements.

Exercise: Review Questions


RQ1: You have a list x that is defined as follows:
x = [ "a", "b", "b" ]

You need to change the second, b to c

Which command should you use?

Ans: x[2] = "c"


RQ2: You have a list x that is defined as follows:

    `x = [ "a", "b", "c" ]`


Which line of Python code do you need to add d at the end of the list x?

Ans: x = x + ["d"]

Note: A list can only be concatenated to another list, and not any otehr type.


RQ3: You have a list x that is defined as follows:

    `x = [ "a", "b", "c", "d" ]`


You decide to remove an element from it by using del:

    `del( x[ 3 ] )`


How does the list x look after this operation?

Ans: [ ""a", "b", "c" ]


Lab : Manipulating Lists


Objective:

  • Manipulating lists.
  • adapting lists.
  • add elements to a list.
  • Removing elements from a list.

1. Replacing list elements, status:


Concept: To replace an element from a list, and assign new values to the subset.

  • Select either, single elements.
  • Or, change the entire list slices at onece.


In [7]:
"""
Problem Definition:

    We, are going to be continue working on our former list "areas".
    
Instructions: 

    + You did a miscalculation when determining the area of the bathroom;
      
        - it's 10.50 square meters instead of 9.50. Can you make the changes?
   
   
    + Make the "areas" list more trendy!
    
        - Change "living room" to "chill zone".
    
"""
# Create the areas list
areas = ["hallway", 11.25, "kitchen", 18.0, "living room",
         20.0, "bedroom", 10.75, "bathroom", 9.50]

# Correct the bathroom area
areas[-1] = 10.50

# Change "living room" to "chill zone"
areas[4] = "chill zone"
areas


Out[7]:
['hallway',
 11.25,
 'kitchen',
 18.0,
 'chill zone',
 20.0,
 'bedroom',
 10.75,
 'bathroom',
 10.5]

2. Extend a list -- 100xp, status:


Concept: We can add elements to a list by using a + operator.

  • Syntax : listA = listB + [ elem1, elem2, elem3, ... , elem(n-1) ]


In [8]:
"""
Problem Definiton:

You just won the lottery, awesome! You decide to build a poolhouse and a garage.
Can you add the information to the areas list?

Instructions: 

    + Use the + operator to paste the list ["poolhouse", 24.5] to the end of
      the "areas" list. Store the resulting list as "areas_1".
      
    + Further extend "areas_1" by adding data on your garage.
      Add the string "garage" and float "15.45".
      Name the resulting list "areas_2".

"""

# Create the areas list and make some changes
areas = ["hallway", 11.25, "kitchen", 18.0, "chill zone", 20.0, 
         "bedroom", 10.75, "bathroom", 10.50]

# Add poolhouse data to areas, new list is areas_1
areas_1 = areas + [ "poolhouse", 24.5 ]


# Add garage data to areas_1, new list is areas_2
areas_2 = areas_1 + [ "garage", 15.45 ]

3. Delete a list element -- 50xp, status:


Concept: We can "delete" elements from a an existing list using a del statement.

  • Syntax: del( list_name[ element_index ] )
  • Note: After deleting an element from a list, the indexes of all the elements after the deleted item change!


In [9]:
"""
Problem Definition:

There was a mistake! The amount you won with the lottery is not that big
after all and it looks like the poolhouse isn't going to happen.
You decide to remove the corresponding string and float from the areas list.

The ; sign is used to place commands on the same line.
The following two code chunks are equivalent:


        + command1; command2   # Same line

        + command1             # Seperate line
          command2
          
Which of the code chuks will do the job for us?

        Ans: del( areas[ -4 : -2 ] )
"""


Out[9]:
"\nProblem Definition:\n\nThere was a mistake! The amount you won with the lottery is not that big\nafter all and it looks like the poolhouse isn't going to happen.\nYou decide to remove the corresponding string and float from the areas list.\n\nThe ; sign is used to place commands on the same line.\nThe following two code chunks are equivalent:\n\n\n        + command1; command2   # Same line\n\n        + command1             # Seperate line\n          command2\n          \nWhich of the code chuks will do the job for us?\n\n        Ans: del( areas[ -4 : -2 ] )\n"

4. Inner Working of lists -- 100xp, status:



In [16]:
"""
Problem Definition:

    + "areas" list already exist in the code, with it's copy, "areas_copy".
    
    + Next, 1st element in the "areas_copy" list is changed, and "areas" pirnted out.
    
    + If we make a change to the "copy", it will affect the "orginal list" too.
    
        - That's because, although the list being different, they have the same
          "reference" to the list!
          
    + To avoid this situation, we have to make an "explicit copy" of "areas"
      list.
      
    + Using "list()" or [ : ]
    
        ------------------------------------------------------------

Instructions:

    + Change the second command, that creates the variable areas_copy,
    
        - such that "areas_copy" is an explicit copy of the "areas".
        
    + Now, changes made to "areas_copy", shouldn't affect the areas.
    
          
"""
# Create list areas
areas = [11.25, 18.0, 20.0, 10.75, 9.50]

# Create areas_copy
areas_copy = list( areas )  # converting areas to an explicit list

# Change areas_copy
areas_copy[0] = 5.0

# Print areas
print("Original list: " + str(areas))
print("Explicit copy: " + str(areas_copy))


Original list: [11.25, 18.0, 20.0, 10.75, 9.5]
Explicit copy: [5.0, 18.0, 20.0, 10.75, 9.5]