Welcome!

List comprehension in Python

Say you have following array:


arr = [ ['Anton', 1.73], ['Marie', 1.66], ['Paul', 1.89] ]

Now, you want to create an array with all persons that are smaller than 1.80m and you also like to have their height in centimeter. This can be pretty nicely implemented with list comprehension:


new_arr = [ [item[0], item[1]*100] for item in arr if item[1] < 1.80]

<< Zurück zur vorherigen Seite