Vault7: CIA Hacking Tools Revealed
Navigation: » Latest version
Owner: User #54198274
Python List Comprehensions
I have received many questions about constructing lists in python from other lists or a set of classes. Python's list comprehensions make it super easy to create lists.
#so let's say I have a list of lists, and I want to create a separate list with only the first elements of my list of lists
listOfLists = [ ['red','cow'],
['blue','fox'],
['orange','flamingo'],
['purple','people eater'],
]
adjectives = [i[0] for i in listOfLists]
nouns = [i[1] for i in listOfLists]
print adjectives
#['red', 'blue', 'orange', 'purple']
print nouns
#['cow', 'fox', 'flamingo', 'people eater']
NOTE: it's important to note that the iterating variable (i, in this case) is used before the for keyword.
pretty cool huh? Yeah, you're probably not super impressed, but list comprehension can get you some pretty short and eloquent bits of code.
#so lets get all the non prime and prime numbers between 1 and 100
nonprimes = [j for i in range(2,8) for j in range(i*2,100,i)]
primes = [i for i in range(2,100) if i not in nonprimes]
Related articles
('contentbylabel' missing)
('details' missing)