0% found this document useful (0 votes)
22 views

Chap08python Lists

List Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage. Lists are created using square brackets: List Items List items are ordered, changeable, and allow duplicate values. List items are indexed, the first item has index [0], the second item has index [1] etc.

Uploaded by

Bushra Km
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Chap08python Lists

List Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage. Lists are created using square brackets: List Items List items are ordered, changeable, and allow duplicate values. List items are indexed, the first item has index [0], the second item has index [1] etc.

Uploaded by

Bushra Km
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Python Lists

Chapter 8
A List is a kind of
Collection
• A collection allows us to put many values in a single “variable”

• A collection is nice because we can carry all many values around in


one convenient package.

friends = [ 'Joseph', 'Glenn', 'Sally' ]

carryon = [ 'socks', 'shirt', 'perfume' ]


What is not a “Collection”
• Most of our variables have one value in them - when we put a new
value in the variable - the old value is over written

$ python
Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53)
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
>>> x = 2
>>> x = 4
>>> print x
4
List Constants
>>> print [1, 24, 76]
• List constants are surrounded by [1, 24, 76]
square brakets and the elements in >>> print ['red', 'yellow', 'blue']
the list are separated by commas. ['red', 'yellow', 'blue']
>>> print ['red', 24, 98.6]
• A list element can be any Python ['red', 24,
object - even another list 98.599999999999994]
>>> print [ 1, [5, 6], 7]
• A list can be empty [1, [5, 6], 7]
>>> print []
[]
We already use lists!
5
4
for i in [5, 4, 3, 2, 1]
3
:
2
print i
1
print 'Blastoff!'
Blastoff
!
Lists and definite loops - best pals

friends = ['Joseph', 'Glenn', 'Sally']


for friend in friends : Happy New Year:
print 'Happy New Year:', friend JosephHappy New Year:
print 'Done!' GlennHappy New Year:
SallyDone!
Looking Inside Lists

• Just like strings, we can get at any single element in a list using an
index specified in square brackets

>>> friends = [ 'Joseph', 'Glenn',


'Sally' ]
Joseph Glenn Sally >>> print friends[1]
0 1 2 Glenn
>>>
Lists are Mutable >>> fruit = 'Banana’
>>> fruit[0] = 'b’
Traceback
TypeError: 'str' object does not
• Strings are "immutable" - we support item assignment
cannot change the contents of >>> x = fruit.lower()
a string - we must make a new >>> print x
string to make any change banana
>>> lotto = [2, 14, 26, 41, 63]
• Lists are "mutable" - we can
>>> print lotto[2, 14, 26, 41, 63]
change an element of a list
>>> lotto[2] = 28
using the index operator
>>> print lotto
[2, 14, 28, 41, 63]
How Long is a List?

>>> greet = 'Hello Bob’


• The len() function takes a list as a
>>> print len(greet)
parameter and returns the number 9
of elements in the list >>> x = [ 1, 2, 'joe', 99]
• Actually len() tells us the number >>> print len(x)
of elements of any set or sequence 4
(i.e. such as a string...) >>>
Using the range function
>>> print range(4)
[0, 1, 2, 3]
• The range function returns a list
>>> friends = ['Joseph', 'Glenn',
of numbers that range from zero 'Sally']
to one less than the parameter >>> print len(friends)
• We can construct an index loop 3
using for and an integer iterator >>> print range(len(friends))
[0, 1, 2]
>>>
A tale of two loops...
>>> friends = ['Joseph', 'Glenn',
'Sally']
friends = ['Joseph', 'Glenn', 'Sally'] >>> print len(friends)
3
for friend in friends : >>> print range(len(friends))
print 'Happy New Year:', friend [0, 1, 2]
>>>
for i in range(len(friends)) :
Happy New Year:
friend = friends[i]
Joseph
print 'Happy New Year:', friend
Happy New Year: Glenn
Happy New Year: Sally
Concatenating lists using +

>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = a + b
• We can create a new list by adding
>>> print c
two exsiting lists together
[1, 2, 3, 4, 5, 6]
>>> print a
[1, 2, 3]
Lists can be sliced using :
>>> t = [9, 41, 12, 3, 74,
15]
>>> t[1:3]
[41,12] Remember: Just like in
>>> t[:4] strings, the second number
[9, 41, 12, 3] is "up to but not including"
>>> t[3:]
[3, 74, 15]
>>> t[:]
[9, 41, 12, 3, 74, 15]
List Methods

>>> x = list()
>>> type(x)<type 'list'>
>>> dir(x)['append', 'count', 'extend', 'index', 'insert', 'pop',
'remove', 'reverse', 'sort']
>>>

http://docs.python.org/tutorial/datastructures.html
Building a list from scratch

>>> stuff = list()


• We can create an empty list
>>> stuff.append('book')
and then add elements >>> stuff.append(99)
using the append method >>> print stuff
• The list stays in order and ['book', 99]
new elements are added at >>> stuff.append('cookie')
the end of the list >>> print stuff
['book', 99, 'cookie']
Is Something in a List?
>>> some = [1, 9, 21, 10,
• Python provides two 16]
operators that let you >>> 9 in some
check if an item is in a list True
>>> 15 in some
• These are logical operators
False
that return True or False
>>> 20 not in some
• They do not modify the list True
>>>
A List is an Ordered Sequence
• A list can hold many items
>>> friends = [ 'Joseph', 'Glenn',
and keeps those items in the
'Sally' ]
order until we do something
>>> friends.sort()
to change the order
>>> print friends
• A list can be sorted (i.e. ['Glenn', 'Joseph', 'Sally']
change its order) >>> print friends[1]
Joseph>>>
• The sort method (unlike in
strings) means "sort yourself"
Built in Functions and Lists
>>> nums = [3, 41, 12, 9, 74,
15]
• There are a number of
>>> print len(nums)
functions built into Python 6
that take lists as >>> print max(nums)
parameters 74>>> print min(nums)
• Remember the loops we 3
built? These are much >>> print sum(nums)
simpler 154
>>> print sum(nums)/len(nums)
25
http://docs.python.org/lib/built-in-funcs.html
Enter a number: 3
total = 0
count = 0 Enter a number: 9
while True : Enter a number: 5
inp = raw_input('Enter a number: ')
if inp == 'done' : break Enter a number: done
value = float(inp) Average: 5.66666666667
total = total + value
count = count + 1

average = total / count


print 'Average:', average
numlist = list()
while True :
inp = raw_input('Enter a number: ')
if inp == 'done' : break
value = float(inp)
numlist.append(value)

average = sum(numlist) / len(numlist)


print 'Average:', average
Best Friends: Strings and Lists
>>> abc = 'With three words’ >>> print stuff
>>> stuff = abc.split() ['With', 'three', 'words']
>>> print stuff >>> for w in stuff :
['With', 'three', 'words'] ... print w
>>> print len(stuff) ...
3 With
>>> print stuff[0] Three
With Words
>>>

Split breaks a string into parts produces a list of strings. We think of these as
words. We can access a particular word or loop through all the words.
>>> line = 'A lot of spaces’
>>> etc = line.split()
>>> print etc['A', 'lot', 'of', 'spaces']
>>>
>>> line = 'first;second;third’
>>> thing = line.split()
>>> print thing['first;second;third']
>>> print len(thing)
1
>>> thing = line.split(';')
>>> print thing['first', 'second', 'third']
>>> print len(thing)
3 When you do not specify a delimiter, multiple
>>>
spaces are treated like “one” delimiter.

You can specify what delimiter character to


use in the splitting.
From [email protected] Sat Jan 5 09:14:16 2008

fhand = open('mbox-short.txt')
Sat
for line in fhand:
Fri
line = line.rstrip()
Fri
if not line.startswith('From ') : continue
Fri
words = line.split()
...
print words[2]

>>> line = 'From [email protected] Sat Jan 5 09:14:16 2008’


>>> words = line.split()
>>> print words
['From', '[email protected]', 'Sat', 'Jan', '5', '09:14:16', '2008']
>>>
The Double Split Pattern
• Sometimes we split a line one way and then grab one of the pieces of
the line and split that piece again

From [email protected]
[email protected] Jan 5 09:14:16 2008

words = line.split()
email = words[1]
pieces = email.split('@') ['stephen.marquard', 'uct.ac.za']
print pieces[1]
'uct.ac.za'
The Double Split Pattern
• Sometimes we split a line one way and then grab one of the pieces of
the line and split that piece again

From [email protected] Sat Jan 5 09:14:16 2008

words = line.split() stephen.marquard uct.ac.za


[email protected]
email = words[1]
pieces = email.split('@') ['stephen.marquard', 'uct.ac.za']
print pieces[1]
'uct.ac.za'
The Double Split Pattern
• Sometimes we split a line one way and then grab one of the pieces of
the line and split that piece again

From [email protected] Sat Jan 5 09:14:16 2008

words = line.split() [email protected]


email = words[1]
pieces = email.split('@') ['stephen.marquard', 'uct.ac.za']
'uct.ac.za'
print pieces[1]
The Double Split Pattern
• Sometimes we split a line one way and then grab one of the pieces of
the line and split that piece again

From [email protected] Sat Jan 5 09:14:16 2008

words = line.split() [email protected]


email = words[1]
pieces = email.split('@') ['stephen.marquard', 'uct.ac.za']
print pieces[1]
'uct.ac.za'
List Summary
• Concept of a collection • Slicing lists

• Lists and definite loops • List methods: append, remove

• Indexing and lookup • Sorting lists

• List mutability • Splitting strings into lists of


words
• Functions: len, min, max, sum
• Using split to parse strings

You might also like