python - List comprehension having wrong length -


i have list seeds , leechs return 19 on asking length using len()
, using these 2 lists list comprehension -

sldiff = [(int(seed)-int(leech)) seed in seeds leech in leechs] 

each element supposed difference between seed , leech(which strings have typecasted)
len(sldiff) returns 361!
questions - why happening , should required sldiff list?

you're doing double list comprehension - ie you're iterating through whole of 'seeds' each entry in 'leechs' (so 19*19, ie 361).

seems want iterate through 1 list, each entry of combination of relevant entry seeds , 1 leechs. zip does:

[(int(seed) - int(leech)) seed, leech in zip(seeds, leechs)] 

Comments