简体   繁体   中英

creating list of empty lists - Python

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.

Does this do what you want?

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

It has the same output as your code

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

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) ]
test = map(lambda x: [], foobar[:len(foobar)/2])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM