This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Make bytes and bytearray maketrans accept dictionaries as first argument as it's done in str
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.7
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: oleksandr.suvorov, serhiy.storchaka, terry.reedy
Priority: normal Keywords:

Created on 2017-09-26 16:04 by oleksandr.suvorov, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (6)
msg303048 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-26 16:12
Could you please provide more information? What the problem you want to solve?
msg303049 - (view) Author: Oleksandr Suvorov (oleksandr.suvorov) Date: 2017-09-26 16:25
str.maketrans in python3 is able to accept just one argument which is a
mapping of translations.
While bytearray and bytes are still using only old way where you should
pass two iterables from and to.

static str.maketrans(x [,y [,z]])
static bytes.maketrans(from, to)
static bytearray.maketrans(from, to)

On Tue, Sep 26, 2017 at 6:12 PM, Serhiy Storchaka <>

> New submission from Serhiy Storchaka:
> Could you please provide more information? What the problem you want to
> solve?
> ----------
> nosy: +serhiy.storchaka
> _______________________________________
> Python tracker <>
> <>
> _______________________________________
msg303051 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-26 16:40
Why do you need this functionality?
msg303053 - (view) Author: Oleksandr Suvorov (oleksandr.suvorov) Date: 2017-09-26 16:46
This change requested just because I find it inconsistent and passing
translation as key-value is much easier to read rather than two lists.
When translation table gets bigger than 10 pairs it gets annoying to match
in the head between from and to while reading it.
Could be marked as minor.

> Python tracker <>
> <>
> _______________________________________
msg303056 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-26 17:17
Inconsistency is a weak argument. It itself is not enough to add a new feature. str.translate() accepts a mapping instead of a string (as it would be for consistency with bytes.translate()) because it would be inconvenient to provide a 1114112-character string. str.maketrans() just compile a mapping in the optimized representation, because using a general mapping is not very efficient.

A bytes object of length 256 already is the most efficient representation of the translation table for bytes. And you can create it from a mapping in Python:

def mymaketrans(mapping):
    table = bytearray(range(256))
    for x, y in mapping.items():
        table[x] = y
    return table
msg303377 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2017-09-30 00:16
Consistency between bytes and strings is not much of a concern here, or the change would have been make for bytes when it was made for strings.  I would not approve the request without knowing who chose not to and why.

I think an example like
t = bytes.maketrans(
is *much* better as it is than as a dict.
Date User Action Args
2022-04-11 14:58:52adminsetgithub: 75775
2021-10-25 21:33:59iritkatrielsetstatus: open -> closed
type: behavior -> enhancement
resolution: rejected
stage: resolved
2017-09-30 00:16:03terry.reedysetnosy: + terry.reedy
messages: + msg303377
2017-09-26 17:17:57serhiy.storchakasetmessages: + msg303056
2017-09-26 16:46:44oleksandr.suvorovsetmessages: + msg303053
2017-09-26 16:40:46serhiy.storchakasetmessages: + msg303051
2017-09-26 16:25:33oleksandr.suvorovsetmessages: + msg303049
2017-09-26 16:12:50serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg303048
2017-09-26 16:04:49oleksandr.suvorovcreate