Back

Python Chrestomathics

Ahead


 

Part 7 — Lists

Lest we go forward — Without knowing about lists — We would be wanting

parchment parchment parchment


Section 7.1 Basics

# create an empty list

values = []

# create some non-empty lists

stuff = [ "abc", 1112, 2.71 ]

numbers = [ 14, 12, 27, 31, 24 ]

# determine lengths

nv = len( values )

ns = len( stuff )

nn = len( numbers )

print( "nv:", nv )

print( "ns:", ns )

print( "nn:", nn )

Its output confirms this intitialization.

nv: 0

ns: 3

nn: 5

# set indices

i = 2

j = 4

Then the below code initializes x and y to 27 and 24.

# access elements

x = numbers[ i ]

y = numbers[ j ]

print( "x:", x )

print( "y:", y )

The output confirms the initialization.

x: 27

y: 24

numbers[ 2 ] = 25

# slice

items = numbers[ 1 : 4 ]

# get min and max

a = min( numbers )

b = max( numbers )

print( "a:", a )

print( "b:", b )

The snippet shows these values in its output.

a: 12

b: 31

alert

Naming gotchas

  • Sometimes programmers will write code like the following, where data is a list of numbers.

min = min( data )

max = min( data )

  • When the code completes, variables min and max are respectively the minium and maximum values in data. They are no longer built-in functions and cannot be used to determine min and maxes of other lists. So, the code is better written as the following.

data_min = min( data )

data_max = min( data )

print( "values:", values )

print( "stuff:", stuff )

print( "numbers:", numbers )

print( "items:", items )

values: []

stuff: ['abc', 1112, 2.71]

numbers: [14, 12, 25, 31, 24]

items: [12, 25, 31]



Section 7.2 — List methods for adding and counting elements

# get example text

reply = input( "Enter text: " )

words = reply.split()

print( "words:", words )

Enter words: better today means better tomorrow

words: ['better', 'today', 'means', 'better', 'tomorrow']

# append a word

words.append( "okay" )

words: ['better', 'today', 'means', 'better', 'tomorrow', 'okay']

# insert a word

words.insert( 3, "a" )

words: ['better', 'today', 'means', 'a', 'better', 'tomorrow', 'okay']

# search words

n1 = words.count( "better" )

n2 = words.count( "e" )

n1: 2

n2: 0

alert

Counting gotcha

  • I suspect some of you might be surprised that the above code reports it did not find an "e" in words. You need to remember that count() was checking for elements equal to "e" and not elements that contained an "e".



Section 7.3 — List methods for deleting elements

# get example text

reply = input( "Enter text: " )

words = reply.split()

reply = input( "Enter index: " )

i = int( reply )

w = input( "Enter word: " )

Enter text: abc acb bac bca cab cba

Enter index: 4

Enter word: bac

words: ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']

i: 0

w: bac

# pop some elements

popped1 = words.pop( i )

popped2 = words.pop( )

popped1: abc

popped2: cba

words after removing popped1 and popped2: ['acb', 'bac', 'bca', 'cab']

# remove element with value w

words.remove( w )

words after removing w: ['acb', 'bca', 'cab']

alert

Removing gotchas

  • If method remove() is not given a list value as its argument, then the statement generates an error when executed.

# clear out all the elements

words.clear()

words after clearing: []



Section 7.4 What’s next


Back

Python Chrestomathics

Ahead

 


© Jim Cohoon. For now, all rights reserved.