Sequence of empty lists

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JP. Baker

    #1

    Sequence of empty lists

    I give up (and have prepared myself for replies telling
    me which search strings to use on Google etc)!

    How *should* I create a sequence of N empty lists (buckets)?

    I obviously can't use

    a = [[]]*N

    and I have found various solutions and am currently using

    a = map(lambda x: [], range(N))

    but can't help feeling that I have missed something obvious.

    nhoJ
    --
    John P Baker
  • Diez B. Roggisch

    #2
    Re: Sequence of empty lists


    seq = [[] for i in xrange(10)]

    --
    Regards,

    Diez B. Roggisch

    Comment

    • Peter Hansen

      #3
      Re: Sequence of empty lists

      Diez B. Roggisch wrote:[color=blue]
      > seq = [[] for i in xrange(10)][/color]

      And the reason you need that approach is given in the FAQ:


      It's a very good idea to read the entire FAQ as soon as you've gotten
      past the very basic Python level, so that you can save yourself
      and others a lot of time stumbling over the traditional problems
      that everyone goes through. You'll learn a lot of useful things
      in the process.

      -Peter

      Comment

      • Paul Rubin

        #4
        Re: Sequence of empty lists

        ccjpb@shark.cse .bris.ac.uk (JP. Baker) writes:[color=blue]
        > How *should* I create a sequence of N empty lists (buckets)?[/color]

        a = [[] for i in xrange(N)]

        Comment

        Working...