Tt
Click this widget to change the font size.
CC
Click this widget to change contrast.

Home Page 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Links | Search | Bio |

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)
Figure 7-1: Creating Python Lists
[1, 2, 3, 4, 5]
['New York', 'Ohio', 'California']
['New York', 12, 12.56]
[0, 1, 2, 3, 4]
[1, 3, 5, 7, 9]
Figure 7-2: Create Python Lists Output

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()
Figure 7-3: Looping Through Lists
for loop output
1
2
3
4
5

while loop output
New York
12
12.56
Figure 7-4: The List Looping Output
# 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")
Figure 7-5: The len() Function
The numbersList list has 5 elements
The mixedList list has 3 elements
Figure 7-6: The len() Output

Python 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)
Figure 7-7: A Mutable List
Before: ['Ohio', 'New York', 'Utah']
After: ['Ohio', 'New York', 'California']
Figure 7-8: A Mutable List Output

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")
Figure 7-9: A Mutable List
Enter a state name: Utah
Utah is in the list

Enter a state name: Wyoming
Wyoming is not in the list
Figure 7-10: A Mutable List Output

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]}")
Figure 7-11: A Two-Dimensional (Nested) List
State: New York, Abbreviation: NY
State: Ohio, Abbreviation: OH
State: Utah, Abbreviation: UT
Figure 7-12: A Two-Dimensional (Nested) List Output

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")
Figure 7-12: Using the count() method for Tuples
The New York is in the statesTupple 2 times
Figure 7-13: Using the 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")
Figure 7-14: Using the index() method for Tuples
Utah is element 2 in the statesTupple
Figure 7-15: Using the index() method for Tuples Output

Help contribute to my OER Resources. Donate with PayPal button via my PayPal account.
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2016-2025 Jim Gerland