Skip to main content

PyRankVote —A Ranked Choice Voting System for Python

PyPI version Test status Coverage Status

PyRankVote is a python library for different ranked-choice voting systems (sometimes called preferential voting systems) created by Jon Tingvold in June 2019.

The following ranking methods are implemented for electing one person/alternative (e.g. electing the chairman to a board):

  • Instant-runoff voting (IRV)—often known as the alternative vote

The following ranking methods are implemented for electing multiple people/alternatives (e.g. electing board members):

  • Single transferable vote (STV)—generally preferred
  • Preferential block voting (PBV)

Different ranking methods

Instant runoff voting (IRV) is a single candidate election method that elects the candidate that can obtain majority support (more than 50%).

Voters rank candidates and are granted one vote. The candidate with fewest votes is removed and this candidate's votes are transferred according to the voters 2nd preference (or 3rd etc).

Preferential block voting (PBV) is a multiple candidate election method that elects candidates that can obtain majority support (more than 50%). PBV tends to elect uncontroversial candidates that agree with each other. Minority group often lose their representation.

Voters rank candidates and are granted as many votes as there are people that should be elected. The candidate with fewest votes are removed and this candidate's votes are transferred according to the voters 2nd preference (or 3rd etc).

Single transferable vote (STV) is a multiple candidate election method that elects candidates based on proportional representation. Minority (and extreme) groups get representation if they have enough votes to elect a candidate. STV is therefore the preferred ranked-choice voting method for parliament elections and most multiple seat elections, but it's more complex than PBV, so it explained last.

Voters rank candidates and are granted as one vote each. If a candidate gets more votes than the threshold for being elected, the candidate is proclaimed as winner. This function uses the Droop quota, where

droop_quota = votes/(seats+1) + 1

If one candidate gets more votes than the threshold the excess votes are transferred to voters that voted for this candidate's 2nd (or 3rd, 4th, etc) alternative. If no candidate gets over the threshold, the candidate with fewest votes is removed. Votes for this candidate is then transferred to voters 2nd (or 3rd, 4th, etc) alternative.

Preferential block voting and Single transferable vote are the same as Instant-runoff voting when only one candidate is elected.

Instant-runoff voting and Preferential block voting are basically the same as exhaustive ballot, the preferred method in Rober's rules of order. The only difference is that in exhaustive ballot voters can adjust their preferences between each round (elimination or election of one candidate).

For more info:

Installation

With pip package manager:

pip install pyrankvote

Usage

import pyrankvote
from pyrankvote import Candidate, Ballot

bush = Candidate("George W. Bush (Republican)")
gore = Candidate("Al Gore (Democratic)")
nader = Candidate("Ralph Nader (Green)")

candidates = [bush, gore, nader]

# Bush have most first choice votes, but because Ralph Nader-voters want
# Al Gore if Nader is not elected, the elected candidate is Al Gore
ballots = [
    Ballot(ranked_candidates=[bush, nader, gore]),
    Ballot(ranked_candidates=[bush, nader, gore]),
    Ballot(ranked_candidates=[bush, nader]),
    Ballot(ranked_candidates=[bush, nader]),
    Ballot(ranked_candidates=[nader, gore, bush]),
    Ballot(ranked_candidates=[nader, gore]),
    Ballot(ranked_candidates=[gore, nader, bush]),
    Ballot(ranked_candidates=[gore, nader]),
    Ballot(ranked_candidates=[gore, nader])
]

# You can use your own Candidate and Ballot objects as long as they implement the same properties and methods
election_result = pyrankvote.instant_runoff_voting(candidates, ballots)

winners = election_result.get_winners()
# Returns: [<Candidate('Al Gore (Democratic)')>]

print(election_result)
# Prints:
"""
ROUND 1
Candidate                      Votes  Status
---------------------------  -------  --------
George W. Bush (Republican)        4  Hopeful
Al Gore (Democratic)               3  Hopeful
Ralph Nader (Green)                2  Rejected

FINAL RESULT
Candidate                      Votes  Status
---------------------------  -------  --------
Al Gore (Democratic)               5  Elected
George W. Bush (Republican)        4  Rejected
Ralph Nader (Green)                0  Rejected
"""

More examples in examples.py

Versions

  • v2.0.6 (2022-10-15) Fix compatibility with new tabular version under Python 3.10
  • v2.0 (2020-04-08): Compact round results and standard STV-procedure
    • Non-backward compatible change: If ballot exhausted, the ballot is now thrown away instead of picking a candidate at random. This is more in line with most RCV-systems. The old practice can be reenabled with pyrankvote.single_transferable_vote(candidates, ballots, pick_random_if_blank=True)
  • v1.0 (2019-09-15): Stable release. First release I assume is stable enough to encourage use.
  • v0.0 (2019-08-09): Pre-release

Popularity

Currently (summer 2022) the library is downloaded round 300 times per month from PyPi (pip). That figure does not include downloads that look like they are coming from bots/mirrors/grab scripts.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Questions?

Feel free to send me an email if you have questions about the project.

License

MIT

Release files for pyrankvote 2.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyrankvote 2.0.6
File Size Uploaded
pyrankvote-2.0.6.tar.gz 13.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyrankvote 2.0.6
File Interpreter ABI Platform
pyrankvote-2.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 27.0 kB

Release files / pyrankvote-2.0.6.tar.gz

Download URL pyrankvote-2.0.6.tar.gz
Size 13.4 kB
Tags Source
SHA-256 checksum
How to use checksums
93b39a0f010d8647bc60a94d2136271cc6979b626a2607ad185368fc505fa142
BLAKE2b-256 checksum
How to use checksums
2500fc2e802fbc05b97755dfed8ee759af9a08cafa6af9b55f099d3e24706a06
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.7

Release files / pyrankvote-2.0.6-py3-none-any.whl

Download URL pyrankvote-2.0.6-py3-none-any.whl
Size 13.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9199080b842d9885f948623a7bfab9c2245c544ed0eb711189e5e2021a38f19c
BLAKE2b-256 checksum
How to use checksums
2a7ad4f37e6b511c2f1cd638f36f097be56a3725c5de6249fc7809db1bc5e100
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.7

Release history Release notifications | RSS feed

This release

2.0.6 This release

2 release files

2.0.5

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.0.9

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.1.4

1 release file

0.1.3

2 release files

0.1.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page