How to remove None when iterating through a list in python -


i have 2 unequal lists , i'm using itertools loop through them , i'm trying use filter function remove none generated in list1 @ end of day contains 2 elements instead of 3 (counting none) keep getting error: type error: nonetype object not iterable

import itertools  list1  = [['a'],['b']] list2 = ['a','b','c']  l = list(itertools.chain(*list1)) print(l)  a, b in itertools.zip_longest((b in list1 b in a),list2):     filter(none, a)     print(a,b) 

not entirely clear want. understand question , comments, want use izip_longest combine lists, without none elements in result.

this filter none zipped 'slices' of lists , print non-none values. note way can not sure whether, e.g., first element in non_none list came first list or second or third.

a = ["1", "2"] b = ["a", "b", "c", "d"] c = ["x", "y", "z"]  zipped in izip_longest(a, b, c):     non_none = filter(none, zipped)     print non_none 

output:

('1', 'a', 'x') ('2', 'b', 'y') ('c', 'z') ('d',) 

btw, filter(none, a) does: filters none values a, i.e. strings "a" , "b" (which not much, contain no none values), until fails last value, none not iterable. also, discards result anyway, not bind variable. filter not alter original list, returns filtered copy!


Popular posts from this blog