Manipulating Lists

Lists are very flexible and have many built-in control functions and methods you can use to manipulate lists.

List Methods

append() method

lst.append() is a frequently used list method, We use it to add an item to the end of an existing list.

Note: to call a method of an object, use the dot notation like: lst.append().

Add another number to a number list:

num = [1,2,3]

# build columns for num list
for i in range(len(num)):
    column(i, -20, num[i], 35)     # white

lst.append(8)
print(num)       # [1,2,3,8]

# build a column indicating the last number in num list
column((len(num)-1), -20, lst[-1], 1)      # brick

The brick column indicates the last new appended number(8) in num

Note: len(num)-1 means the index number of the last item num[-1] indicates the value of the last item

append() makes a new list

It's easy to understand using append() to add items to the end of an empty list could be a way to create a new list:

# make a list grow
my_lst = []
for i in range(5):
    my_lst.append(i)

print (my_lst)        # [0,1,2,3,4]

More List Methods lst.remove(item), removes an item from list lst.pop(), removes and returns last item from list
lst.index(item) lst.insert(indexNumber, newItem)

lst.reverse(), reverse the order of items in list lst.sort(), sort items in list in ascending order

Check out some more list methods in examples:

l = [2,3,4,5,6,7]

# remove(item), removes an item from list
l.remove(7)   
print(l)      # [2,3,4,5,6]

# pop(), removes and returns last item from list   
l.pop()    
print(l)      # [2,3,4,5]

# insert(index, item),  inserts an item into list at index number 
l.insert(2, 10)        # insert 10 at index 2  
print(l)               # [2,3, 10, 4,5]

# index(item), returns the index of the first appearance of an item in list.

m = lst0.index(4)    
print( m )             # 3 (item 4 is at index 3)

For the next two list methods, reverse() and sort(), let's use CodeCraft to demo the effects:

# original list, white columns
t = [2,4,1,8,5,6,7]
for i in range( len(t) ):
    column(i, -20, t[i], 35)      # white 

# Reverses items in the list
t.reverse()   
for i in range( len(t) ):
    column(i+8, -20, t[i], 72)  # purple 

# sort items in list    
t.sort()
for i in range( len(t) ):
    column(i+18, -20, t[i], 71 )     # pink

See the picture, the purple columns mirror the white columns, the pink columns reorder the list in ascending order.

List functions

max(lst) and min(lst)

Python includes some built-in list functions, other then len() function, max() and min() are also frequently used, they return item from the list with max(or min) value.

Example:

Let's build white columns representing a number list, then highlight the maximum value and minimum value in colors(max:red, min:lime green)

s = [ 5,3,12,7,2,8]
for i in range( len(s) ):
    column(i, -20, s[i], 35 )   # white 

column(s.index(min(s)), -20, min(s), 45)   # lime, min

column(s.index(max(s)), -20, max(s), 50)    # red, max

Note: s.index(min(s)) means find the minimum value in the number list s, then return the index of that minimum element.

results matching ""

    No results matching ""