Lists & Loops — Part 1

In the next few lessons, we’re going to learn about a Python data collection type called a list and a way of working with lists called a for loop, and we’re going to draw on The Bellevue Almshouse Dataset, created by historian and DH scholar Anelise Shrout. This dataset includes information about Irish-born immigrants who were admitted to New York City’s Bellevue Almshouse in the 1840s.

Preview The Bellevue Almshouse Dataset

date_in first_name last_name age disease profession gender children
0 1847-04-17 Mary Gallagher 28.0 recent emigrant married f Child Alana 10 days
1 1847-04-08 John Sanin (?) 19.0 recent emigrant laborer m Catherine 2 mo
2 1847-04-17 Anthony Clark 60.0 recent emigrant laborer m Charles Riley afed 10 days
3 1847-04-08 Lawrence Feeney 32.0 recent emigrant laborer m Child
4 1847-04-13 Henry Joyce 21.0 recent emigrant NaN m Child 1 mo
5 1847-04-14 Bridget Hart 20.0 recent emigrant spinster f Child
6 1847-04-14 Mary Green 40.0 recent emigrant spinster f And child 2 months
7 1847-04-19 Daniel Loftus 27.0 destitution laborer m NaN
8 1847-04-10 James Day 35.0 recent emigrant laborer m NaN
9 1847-04-10 Margaret Farrell 30.0 recent emigrant widow f NaN
10 1847-04-10 Bridget Day 30.0 recent emigrant married f NaN
11 1847-04-10 Anthony Day 0.5 recent emigrant NaN m NaN
12 1847-04-07 James Collins 22.0 recent emigrant laborer m NaN
13 1847-04-07 Thomas Collins 21.0 recent emigrant laborer m NaN
14 1847-04-07 Pat Whalen 25.0 recent emigrant laborer m NaN
15 1847-04-17 Dan Delany 10.0 typhus NaN m NaN
16 1847-04-09 Catherine O'Harra 23.0 recent emigrant married f NaN
17 1847-04-09 Damiel O'Harra 25.0 recent emigrant laborer m NaN
18 1847-04-12 Margaret Delaney 26.0 recent emigrant married f NaN
19 1847-04-12 Michael Delany 3.0 recent emigrant NaN m NaN

We’re using the Bellevue Almshouse data to practice lists and loops because we want to think deeply about the consequences of reducing human life to data, even at this early stage in our Python journey. This immigration data, as Shrout argues in her essay “(Re)Humanizing Data: Digitally Navigating the Bellevue Almshouse,” was “produced with the express purpose of reducing people to bodies; bodies to easily quantifiable aspects; and assigning value to those aspects which proved that the marginalized people to who they belonged were worth less than their elite counterparts.”

As we work through the lesson below, reflect about the categories that these Irish immigrants were slotted into by the NYC government. What should we make of the fact that Python, as a programming language, doesn’t understand the meaning or historical context of this data? How can we nevertheless use Python to better understand this history and to interrogate power?


Lists

In the previous lesson, we used individual variables to represent some of the demographic information about the 19th century Irish immigrants featured in the Bellevue Almshouse data, such as names.

person1_name = 'Marry Gallagher'
person2_name = 'John Sanin (?)'

It’s typically more useful, however, to create a collection of values rather than individual variables.

One of the most common Python data collections is a list. By using a list, we can put the names of the people featured in the dataset into a single collection.

names = ['Mary Gallagher', 'John Sanin(?)', 'Anthony Clark', 'Margaret Farrell']
type(names)
list

A list is always enclosed by square brackets [] and accepts items in a row separated by commas (,). A list can contain any combination of Python data types.

ages = [28, 19, 60, 30]
type(ages)
list

Index

You can index a list like you would index a string.

names = ['Mary Gallagher', 'John Sanin(?)', 'Anthony Clark', 'Margaret Farrell']

For example, if we wanted to pull out the first item in our names list, we could put square brackets and our desired index number immediately after the list. Just like with strings, the Python index begins with 0.

names[0]
'Mary Gallagher'
names[3]
'Margaret Farrell'

You can also reverse index a list.

names[-1]
'Margaret Farrell'

Slice

You can also slice lists like you can slice a string.

more_names = ['Unity', 'Catherine', 'Thomas', 'William', 'Patrick',
              'Mary Anne', 'Morris', 'Michael', 'Ellen', 'James']

The full notation and options for slicing a list are as follows:

your_list[start:stop:step]

By putting specific index numbers between these colons, you can slice the list at certain starting and stopping points, and you can also “step” by different amounts—that is, you can jump by a certain number through the list and only take every nth item in the list (e.g. every 3rd item).

Slice

Explanation

Output

 more_names[:2]

Slice list up to 2nd item

['Unity', 'Catherine']
 more_names[2:]

Slice from 2nd item to end of list

  ['Thomas',
 'William',
 'Patrick',
 'Mary Anne',
 'Morris',
 'Michael',
 'Ellen',
 'James']
 more_names[::3]

Slice from 0 to end of list, stepping by 3

['Unity', 'William', 'Morris', 'James']

Start

more_names = ['Unity', 'Catherine', 'Thomas', 'William', 'Patrick',
              'Mary Anne', 'Morris', 'Michael', 'Ellen', 'James']

Here’s how we would slice the list starting from the 2nd item of the list until the end.

more_names[2:]
['Thomas',
 'William',
 'Patrick',
 'Mary Anne',
 'Morris',
 'Michael',
 'Ellen',
 'James']

You might imagine the above as:

more_names[start=2:stop=none:step=none]

Remember that the Python index starts with 0 so the 2nd item is really the 3rd item.

more_names[2]
'Thomas'

Reverse

more_names = ['Unity', 'Catherine', 'Thomas', 'William', 'Patrick',
              'Mary Anne', 'Morris', 'Michael', 'Ellen', 'James']

Because we can reverse index a list from the end to the beginning, we can also slice a list by starting from the 2nd to last item until the end.

more_names[-2:]
['Ellen', 'James']

You might imagine the above as:

more_names[start=-2:stop=none:step=none]

Stop

more_names = ['Unity', 'Catherine', 'Thomas', 'William', 'Patrick',
              'Mary Anne', 'Morris', 'Michael', 'Ellen', 'James']

Here’s how we would slice the list starting from 0 and ending at the 2nd item in the list.

more_names[:2]
['Unity', 'Catherine']

You might imagine the above as:

more_names[start=0:stop=2:step=none]

Step

You can also index by a certain number of steps.

Here’s how we could index every third item in the list.

more_names[::3]
['Unity', 'William', 'Morris', 'James']

You might imagine the above as:

more_names[start=0:stop=0:step=3]

List Methods

Lists also have a number of special methods that can be used with them, such as a method for adding items to a list.

List Method

Explanation

list.append(another_item)

adds new item to end of list

list.extend(another_list)

adds items from another_list to list

list.remove(item)

removes first instance of item

list.sort(reverse=False)

sort the order of list

list.reverse()

reverses order of list

Add Items To List

List Method

Explanation

list.append(another_item)

adds new item to end of list

names = ['Mary Gallagher', 'John Sanin(?)', 'Anthony Clark', 'Margaret Farrell']
names.append("Lawrence Feeney")
names
['Mary Gallagher',
 'John Sanin(?)',
 'Anthony Clark',
 'Margaret Farrell',
 'Lawrence Feeney']

Sort List

List Method

Explanation

list.sort(reverse=False)

sort the order of list

names.sort()
names
['Anthony Clark',
 'John Sanin(?)',
 'Lawrence Feeney',
 'Margaret Farrell',
 'Mary Gallagher']
ages.sort()
ages
[19, 28, 30, 60]
ages.sort(reverse=True)
ages
[60, 30, 28, 19]

Extend List With Another List

List Method

Explanation

list.extend(another_list)

adds items from another_list to list

names.extend(ages)
names
['Anthony Clark',
 'John Sanin(?)',
 'Lawrence Feeney',
 'Margaret Farrell',
 'Mary Gallagher',
 60,
 30,
 28,
 19]

For Loops

One of the best ways to work with a list is with for loops. This is a way of considering each item in the list or “iterating” through the list.

names = ['Mary Gallagher', 'John Sanin(?)', 'Anthony Clark', 'Margaret Farrell']
for name in names:
    print(name)
Mary Gallagher
John Sanin(?)
Anthony Clark
Margaret Farrell

A basic basic for loop will consist of two lines:

  • On the first line, you type the English word for, a new variable name for each item in the list, the English word in, the name of the list, and a colon (:)

  • On the second line, you indent and write an instruction or “statement” to be completed for each item in the list

for name in names:
    print(f"Person's name is {name}")
Person's name is Mary Gallagher
Person's name is John Sanin(?)
Person's name is Anthony Clark
Person's name is Margaret Farrell
for x in names:
    print(f"Person's name is {x}")
Person's name is Mary Gallagher
Person's name is John Sanin(?)
Person's name is Anthony Clark
Person's name is Margaret Farrell
ages = [28, 19, 60, 30]
for age in ages:
    if age > 30:
        print("Person is less than 30 years old")
    else:
        print("Person is more than 30 years old")
Person is more than 30 years old
Person is more than 30 years old
Person is less than 30 years old
Person is more than 30 years old
for age in ages:
    print(age * 2)
56
38
120
60

Exercises

professions = ['married', 'laborer', 'widow', 'laborer', ]
child_status = ['Child Alana 10 days', 'Catherine 2 mos', '', 'Charles Riley afed 10 days' ]
gender = ['f', 'm', 'w', 'm']

Exercise 1

Extract the second item in the list professions.

##Your Code Here
'laborer'

Exercise 2

Add the item “spinster” to your professions list, then print the list.

##Your Code Here
##Your Code Here
['married', 'laborer', 'widow', 'laborer', 'spinster']

Exercise 3

Make a for loop that considers each item in the professions list and prints “Person’s profession is ___”

##Your Code Here
    ##Your Code Here
Person's profession is married
Person's profession is laborer
Person's profession is widow
Person's profession is laborer
Person's profession is spinster

Exercise 4

Extract the fourth item in the child_status list.

##Your Code Here
'Charles Riley afed 10 days'

Exercise 5

Make a for loop that considers each item in the child_status list and prints “Person has child” if the person has a child and “Person does not have child” if not

##Your Code Here
  ##Your Code Here
       ##Your Code Here
    ##Your Code Here
        ##Your Code Here
Person has child
Person has child
Person does not have child
Person has child

Exercise 6

Add an item to the list gender called “not known”

##Your Code Here

Exercise 7

Make a for loop that considers each item in the gender list and prints “Person is male” if the person is male, “Person is female” if the person is female, and “Person’s gender is not known” if unknown

 ##Your Code Here
     ##Your Code Here
         ##Your Code Here
     ##Your Code Here
         ##Your Code Here
     ##Your Code Here
         ##Your Code Here
Person is female
Person is male
Person is male
Person's gender is not known