Guide to Python (University at Buffalo Version)
Chapter 7: Working With Lists and Tuples
Python Sequences
In Python, sequences are a collection of items of data (strings, numbers, objects, etc.). Three (3) main types of sequences are lists, and tuples. See arrays vs lists vs tuples for a comparision of these three (3) sequence types.
Python Lists
Python provides many functions and methods for creating and working with lists. Below are a few examples.
# lists.py # a list of numbers numbersList = [1, 2, 3, 4, 5] print(numbersList) # a list of strings stringsList = ["New York", "Ohio", "California"] print(stringsList) # a list of mixed data types mixedList = ["New York", 12, 12.56] print(mixedeList) # a list of a range of numbers rangeList = list(range(5)) print(rangeList) # a list of a range of odd numbers between 1 and 10 rangeList = lisr(range(1, 10, 2)) print(rangeList)
[1, 2, 3, 4, 5] ['New York', 'Ohio', 'California'] ['New York', 12, 12.56] [0, 1, 2, 3, 4] [1, 3, 5, 7, 9]
Looping Through a List
You can use the for
and while
statements to loop through a list.
# list_loop.py # a list of numbers numbersList = [1, 2, 3, 4, 5] print("for loop output") for num in numbersList: print(num) print() # a list of mixed data types mixedList = ["New York", 12, 12.56] print("while loop output") i = 0 while i < 3: print(mixedList[i]) i += 1 print()
for loop output 1 2 3 4 5 while loop output New York 12 12.56
# list_len.py # a list of numbers numbersList = [1, 2, 3, 4, 5] print("The numbersList list has", len(numbersList), "elements") # a list of mixed data types mixedList = ["New York", 12, 12.56] print("The mixedList list has", len(numbersList), "elements")
len()
FunctionThe numbersList list has 5 elements The mixedList list has 3 elements
len()
OutputPython Lists are Mutable
Python lists are mutable (unlike tuples). That means you can change any element at any time.
# list_mut.py statesList = ["Ohio", "New York", "Utah"] print("Before:", statesList) statesList[2] = "California" print("After:", statesList)
Before: ['Ohio', 'New York', 'Utah'] After: ['Ohio', 'New York', 'California']
Python Lists are Searchable
You can use the Python lists for/in
statment to search a list.
# list_in.py debug = 0; statesList = ["Ohio", "New York", "Utah"] userInput = input("Enter a state name: ") if (debug): print(userInput) for s in statesList: if (debug): print(s) if (s == userInput): print(userInput, "is in the list") break # Leave the for loop if we find a match else: print(userInput, "is not in the list")
Enter a state name: Utah Utah is in the list Enter a state name: Wyoming Wyoming is not in the list
Python Two-Dimensional (Nested) Lists
Python also allows Two-Dimensional (Nested) lists.
# list_2d.py statesList = [["New York", "NY"], ["Ohio", "OH"], ["Utah", "UT"]] for s in statesList: print(f"State: {s[0]}, Abbreviation: {s[1]}")
State: New York, Abbreviation: NY State: Ohio, Abbreviation: OH State: Utah, Abbreviation: UT
Python Tuples
Python tuples are very similar to Lists. However, tuples are not mutable (their elements cannot be changed). Python provides functions and methods for creating and working with tuples. Below are a examples of the cout()
and index()
methods.
# tupple_count.py statesTupple = ["New York", "Ohio", "Utah", "New York"] print(f"New York is in the statesTupple {statesTupple.count('New York')} times")
count()
method for TuplesThe New York is in the statesTupple 2 times
count()
method for Tuples# tupple_index.py statesTupple = ["New York", "Ohio", "Utah", "New York"] print(f"Utah is element {statesTupple.index('Utah')} in the statesTupple")
index()
method for TuplesUtah is element 2 in the statesTupple
index()
method for Tuples Output