Python List and Tuples
Lists and Tuples within python are use pretty much all over the place when you are getting down to the nitty-gritty…
Lists
A list is best explained as an ordered collection of zero or more elements. An element of a list can be any sort of object. You can write lists as a comma-separated collection of values enclosed in square brackets.
For an idea:
FibonacciList=[1,1,2,3,5,8]
FishList=[1,2,”Fish”] # Lists can contain various types.
AnotherList=[1,2,FishList] # Lists can include other lists.
YetAnotherList=[1,2,3,] # Trailing commas are ok.
RevengeOfTheList=[] # The empty list
Tuples
A tuple is similar to a list. The difference is that a tuple is immutable—it cannot be modified. You enclose tuples in parentheses instead of brackets.
For an example of how Tuples work:
FirstTuple=(“spam”,”spam”,”bacon”,”spam”)
SecondTuple=() # The empty tuple
AloneTuple=(5,) # Trailing comma is *required*, since (5) is # just a number-in-parens, not a tuple.
Cutting it All Apart
Lists are ordered, so each list element has an index. You can access an element with the syntax listname[index].
Note that index numbering begins with zero:
>>> FoodList=[“Spam”,”Egg”,”Sausage”]
>>> FoodList[0]
‘Spam’>>> FoodList[2]
‘Sausage’>>> FoodList[2]=”Spam” # Modifying list elements in place
>>> FoodList
[‘Spam’, ‘Egg’, ‘Spam’]
Sometimes it’s easier to count from the end of the list backwards. You can access the last item of a list with listname[-1], the second-to-last item with listname[-2], and so on.
You can access a sublist of a list via the syntax listname[start:end]. The sublist contains the original list elements, starting with index start, up to (but not including) index end. Both start and end are optional; omitting them makes Python go all the way to the beginning (or end) of the list.
For an example:
>>>PrimaryWordList=[“And”,”now”,”for”,”something”,”completely”,
“different”]>>> PrimaryWordList[0:2] # From index 0 to 2 (not including 2)
[‘And’, ‘now’]
10 Part I ✦ The Python Language>>> PrimaryWordList[2:5]
[‘for’, ‘something’, ‘completely’]>>> PrimaryWordList[:-1] # All except the last
[‘And’, ‘now’, ‘for’, ‘something’, ‘completely’]
Substrings
Lists, tuples, and strings are all sequence types. Sequence types all support indexed access. So, taking a substring in Python is as simple as it comes:
>>> Word=”pig”
>>> PigLatinWord=Word[1:]+Word[0]+”ay”
>>> PigLatinWord
‘igpay’
Immutable types
Tuples and strings are immutable types.
Modifying them in place is not allowed:
FirstTuple[0]=”Egg” # Object does not support item assignment. You can switch between tuples and lists using the tuple and list functions.
So, although you cannot edit a tuple directly, you can create a new-and-improved tuple:
>>> FoodTuple=(“Spam”,”Egg”,”Sausage”)
>>> FoodList=list(FoodTuple)
>>> FoodList
[‘Spam’, ‘Egg’, ‘Sausage’]>>> FoodList[2]=”Spam”
>>> NewFoodTuple=tuple(FoodList)
>>> NewFoodTuple
(‘Spam’, ‘Egg’, ‘Spam’)
Now I am hungry so WTF it’s time for some Green Eggs and Ham.

