-1
test = [[None for e in range(1)] for e in range(len(foobar)/2)]
for delete in range(0,len(test)):
        del test[delete][0]

This is not the most pythonic way to create an empty list

would you suggest something else?

I know that this question explains it. but this is not a duplicate of the previous one as you can see.

Community
  • 1
  • 1
pistal
  • 2,310
  • 13
  • 41
  • 65

4 Answers4

7

Does this do what you want?

test = [[] for e in range(len(foobar)/2)]

It has the same output as your code

Brionius
  • 13,858
  • 3
  • 38
  • 49
  • 2
    @pistal: This would create a list containing ten references to the same list object. If you added anything to any of the ten lists, it appears to be added to all of them, since they are all the same object. – Sven Marnach Aug 13 '13 at 12:39
  • Doesn't the question contradict that or maybe i am wrong. It says you can create lists that way. – pistal Aug 13 '13 at 12:40
  • 1
    @pistal Also `[]*10` concatenates an empty list with itself 10 times, which results in...an empty list. `[[]]*10` is a little closer, but it has the problem that @SvenMarnach describes. – Brionius Aug 13 '13 at 12:41
0
test = [[None for e in range(1)] for e in range(len(foobar)/2)]

can be simplified to:

test = [[] for i in xrange(len(foobar)/2)]

Also don't forget to use xrange instead of range, as it returns a generator and not a complete list

tobspr
  • 8,200
  • 5
  • 33
  • 46
0

An empty list:

test = []

Unless we're missing something ;-)

Or you want an list of empty lists of a given length?

n = 100
test = [ [] for i in range(0,n) ]
juandesant
  • 703
  • 8
  • 16
0
test = map(lambda x: [], foobar[:len(foobar)/2])
Bleeding Fingers
  • 6,993
  • 7
  • 46
  • 74