Merge lp:~brunogirin/python-snippets/bzhbranch into lp:~jonobacon/python-snippets/trunk

Proposed by Bruno Girin
Status: Merged
Merged at revision: not available
Proposed branch: lp:~brunogirin/python-snippets/bzhbranch
Merge into: lp:~jonobacon/python-snippets/trunk
Diff against target: 95 lines (+91/-0)
1 file modified
pythoncore/lists101.py (+91/-0)
To merge this branch: bzr merge lp:~brunogirin/python-snippets/bzhbranch
Reviewer Review Type Date Requested Status
Jono Bacon Pending
Review via email: mp+20282@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Bruno Girin (brunogirin) wrote :

Added a list 101 that includes examples of most operations on lists.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'pythoncore/lists101.py'
2--- pythoncore/lists101.py 1970-01-01 00:00:00 +0000
3+++ pythoncore/lists101.py 2010-02-27 22:07:16 +0000
4@@ -0,0 +1,91 @@
5+#!/usr/bin/env python
6+#
7+# [SNIPPET_NAME: Lists 101]
8+# [SNIPPET_CATEGORIES: Python Core]
9+# [SNIPPET_DESCRIPTION: Basic and not so basic list operations]
10+# [SNIPPET_AUTHOR: Bruno Girin <brunogirin@gmail.com>]
11+# [SNIPPET_LICENSE: GPL]
12+
13+# This snippet demonstrates how the basics on lists: how to create, add,
14+# remove items, get items or slices, sort, etc.
15+
16+#
17+# First, let's create simple list
18+#
19+print "Create a simple list"
20+simpleList = ["Karmic", "Lucid", "Hardy", "Jaunty", "Intrepid"]
21+# print it
22+print simpleList
23+#
24+# Interrogate the list
25+#
26+print "\nInterrogate the list"
27+# get item 3: lists start counting at 0 so it should be Jaunty
28+print simpleList[3]
29+# we can also get a slice
30+print simpleList[2:4]
31+# get the first three items
32+print simpleList[:3]
33+# or all items from number index 3 (which is the fourth item) to the end
34+print simpleList[3:]
35+# we can also take every other item, as a slice is defined
36+# like this: start:stop:step
37+print simpleList[::2]
38+# get the length of the list
39+print len(simpleList)
40+# we can get the index of an item in the list
41+print simpleList.index("Hardy")
42+# and when the list doesn't contain the item, we get an error that we can catch
43+try:
44+ print simpleList.index("Feisty")
45+except ValueError:
46+ print "The list doesn't contain the item Feisty"
47+#
48+# Modify the list
49+#
50+print "\nModify the list"
51+# add another item
52+simpleList.append("Twisty")
53+print simpleList
54+# oops! let's sort this out by replacing in place
55+simpleList[5] = "Gutsy"
56+print simpleList
57+# extend the list with another one
58+otherList = ["Edgy", "Breezy"]
59+simpleList.extend(otherList)
60+print simpleList
61+# remove an item from the list (Hardy should not be in the list anymore)
62+del simpleList[2]
63+print simpleList
64+# insert an item in the middle of the list
65+simpleList.insert(4, "Hardy")
66+print simpleList
67+# remove an item by its value rather than its index
68+simpleList.remove("Edgy")
69+print simpleList
70+#
71+# Create modified copies of the list
72+#
73+print "\nCreate modified copies of the list"
74+# sort it
75+print sorted(simpleList)
76+# join it to produce a custom print
77+print ' => '.join(sorted(simpleList))
78+# lists can contain the same item several times so if we add Lucid again:
79+simpleList.append("Lucid")
80+# we have it twice in the list (easier to see if we sort it)
81+print sorted(simpleList)
82+# but we can get round that by transforming it into a set, which is a list
83+# with no duplicates; and of course we can also sort the set
84+print sorted(set(simpleList))
85+#
86+# Iterate over the list
87+#
88+print "\nIterate over the list"
89+for i in simpleList:
90+ print i.upper()
91+# but if we want to create another list by applying the same expression to
92+# each item, we can use a list comprehension
93+upList = [i.upper() for i in sorted(set(simpleList))]
94+print upList
95+

Subscribers

People subscribed via source and target branches