简体   繁体   中英

Python: appending a list to over 255 elements

I have a list which i append every time i run a for-loop. This loop runs for more than 255 times but i cannot append the list beyond 255 elements. How can i fix this problem?

for x in y:  
        a = json.dumps(b)
        c.append(a)

There are many ways to do this

Slicing:

c = [json.dumps(b) for x in y[0:255]]

Or using enumerate:

for ind, x in enumerate(y):
    if ind == 255:
         break
    c.append(json.dumps(b))

Or you could use a while loop, it's easy because len is O(1) for a list :

while len(c) < 255:
    c.append(json.dumps(b))

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