Pythonic list/tuple/dict layout?

Discussion in 'Python' started by Akira Kitada, Jan 25, 2009.

  1. Akira Kitada

    Akira Kitada Guest

    Hi,

    There is more than one way to write a list/tuple/dict in Python,
    and actually different styles are used in standard library.
    As a hobgoblin of little minds, I rather like to know which style is
    considered "Pythonic"
    in the community.

    I collected common layout from existing code and pasted them below.
    My vote would go to d1. How about yours?

    If there is consensus on this, that might be worth being included in PEP 8.

    Thanks,

    """
    d1 = {
    0: "ham",
    1: "jam",
    2: "spam",
    3: "alot",
    4: "knights",
    5: "who",
    6: "say",
    7: "ni",
    8: "dead",
    9: "parrot",
    }

    d2 = {
    0: "ham",
    1: "jam",
    2: "spam",
    3: "alot",
    4: "knights",
    5: "who",
    6: "say",
    7: "ni",
    8: "dead",
    9: "parrot",}

    d3 = {0: "ham",
    1: "jam",
    2: "spam",
    3: "alot",
    4: "knights",
    5: "who",
    6: "say",
    7: "ni",
    8: "dead",
    9: "parrot",
    }

    d4 = {0: "ham",
    1: "jam",
    2: "spam",
    3: "alot",
    4: "knights",
    5: "who",
    6: "say",
    7: "ni",
    8: "dead",
    9: "parrot",}

    d5 = {0: "ham",
    1: "jam",
    2: "spam",
    3: "alot",
    4: "knights",
    5: "who",
    6: "say",
    7: "ni",
    8: "dead",
    9: "parrot",
    }
    """
    Akira Kitada, Jan 25, 2009
    #1
    1. Advertising

  2. Akira Kitada

    Dan Bishop Guest

    On Jan 25, 2:18 am, Akira Kitada <> wrote:
    > Hi,
    >
    > There is more than one way to write a list/tuple/dict in Python,
    > and actually different styles are used in standard library.
    > As a hobgoblin of little minds, I rather like to know which style is
    > considered "Pythonic"
    > in the community.
    >
    > I collected common layout from existing code and pasted them below.
    > My vote would go to d1. How about yours?
    >
    > If there is consensus on this, that might be worth being included in PEP 8.
    >
    > Thanks,
    >
    > """
    > d1 = {
    >     0: "ham",
    >     1: "jam",
    >     2: "spam",
    >     3: "alot",
    >     4: "knights",
    >     5: "who",
    >     6: "say",
    >     7: "ni",
    >     8: "dead",
    >     9: "parrot",
    >
    > }
    > [snip]


    I use d1.

    Wow! A Python debate over curly brace placement! Imagine that!
    Dan Bishop, Jan 25, 2009
    #2
    1. Advertising

  3. Akira Kitada

    Akira Kitada Guest

    > Wow! A Python debate over curly brace placement! Imagine that!

    PEP8 even deals with tabs vs spaces, where to put a blank line, etc :)
    Akira Kitada, Jan 25, 2009
    #3
  4. On Sun, 25 Jan 2009 17:18:28 +0900, Akira Kitada wrote:

    > Hi,
    >
    > There is more than one way to write a list/tuple/dict in Python, and
    > actually different styles are used in standard library. As a hobgoblin
    > of little minds, I rather like to know which style is considered
    > "Pythonic"
    > in the community.
    >
    > I collected common layout from existing code and pasted them below. My
    > vote would go to d1. How about yours?


    All of them.

    BTW, there's no need to use such large examples. Three items per dict
    would be sufficient to illustrate the styles, using ten items doesn't add
    anything useful to the discussion.



    --
    Steven
    Steven D'Aprano, Jan 25, 2009
    #4
  5. Akira Kitada

    Akira Kitada Guest

    > BTW, there's no need to use such large examples. Three items per dict
    > would be sufficient to illustrate the styles, using ten items doesn't add
    > anything useful to the discussion.


    I worried to be told
    'you can make it in a line like {"ham": "jam", "spam": "alot"}'
    ;)
    Akira Kitada, Jan 25, 2009
    #5
  6. Akira Kitada

    Akira Kitada Guest

    > These are the only two that follow PEP 8; the others don't have
    > four-space indent levels.


    In those examples, the following sentence in PEP 8 would be applied.

    "Make sure to indent the continued line appropriately."

    > I actually use this style:
    >
    > foo = {
    > 0: 'spam',
    > 1: 'eggs',
    > 2: 'beans',
    > }
    >
    > because that makes it clear that *all* the indented lines are a
    > continuation of the same statement, just like a suite of statements
    > are all uniformly indented under (e.g.) a function definition.
    >
    > It seems that the author of the Python editing mode in Emacs agrees
    > with me too (the style, at least, if not the reasoning), which makes
    > my programming life easier.


    Yes, it does, but people around me tend to prefer d1 style to that one.
    One purpose of this silly thread is to figure out which is most popular one...
    Akira Kitada, Jan 25, 2009
    #6
  7. d1
    Rhamphoryncus, Jan 25, 2009
    #7
  8. Akira Kitada

    Mark Wooding Guest

    Akira Kitada <> writes:

    > I collected common layout from existing code and pasted them below.
    > My vote would go to d1. How about yours?


    It seems that I use both d1 and d4, though in both cases I omit the
    trailing commas. I use d1 when each item is on a separate line, and d4
    when I'm packing them onto multiple lines.

    e.g.,

    op = XT.make_optparse \
    ([('E', 'error',
    {'action': 'store_const', 'dest': 'type', 'const': 'error',
    'help': "Mark the window as reporting an error."}),
    ## ...
    ('t', 'title',
    {'dest': 'title',
    'help': "Set the window's title string."})],
    version = VERSION,
    usage = '%prog [-EIQWm] [-t TITLE] [-d HEADLINE] '
    'MESSAGE [BUTTONS...]')

    and

    service_info = [('watch', T.VERSION, {
    'adopted': (0, 0, '', cmd_adopted),
    'kick': (1, 1, 'PEER', cmd_kick)
    })]

    In this latter case, were I defining multiple services, I'd indent it
    differently:

    service_info = [
    ('watch', T.VERSION, {
    'adopted': (0, 0, '', cmd_adopted),
    'kick': (1, 1, 'PEER', cmd_kick)
    }),
    ##...
    ]

    -- [mdw]
    Mark Wooding, Jan 25, 2009
    #8
  9. Akira Kitada

    Aahz Guest

    In article <>,
    Ben Finney <> wrote:
    >
    >I actually use this style:
    >
    > foo = {
    > 0: 'spam',
    > 1: 'eggs',
    > 2: 'beans',
    > }
    >
    >because that makes it clear that *all* the indented lines are a
    >continuation of the same statement, just like a suite of statements
    >are all uniformly indented under (e.g.) a function definition.


    Ditto
    --
    Aahz () <*> http://www.pythoncraft.com/

    Weinberg's Second Law: If builders built buildings the way programmers wrote
    programs, then the first woodpecker that came along would destroy civilization.
    Aahz, Feb 1, 2009
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    561
    John Timney \(MVP\)
    Jun 19, 2006
  2. Pierre Fortin

    list/tuple to dict...

    Pierre Fortin, Sep 15, 2004, in forum: Python
    Replies:
    17
    Views:
    660
    Pierre Fortin
    Sep 17, 2004
  3. Carl J. Van Arsdall
    Replies:
    4
    Views:
    488
    Bruno Desthuilliers
    Feb 7, 2006
  4. Davy
    Replies:
    3
    Views:
    1,880
    Wildemar Wildenburger
    Nov 7, 2007
  5. Jeff Nyman
    Replies:
    8
    Views:
    370
    Terry Reedy
    Jun 5, 2008
Loading...

Share This Page