├── README.md
└── list_comprehension.py
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # Python List Comprehension
3 | As we know that there exist a Pythonic technique of constructing a list from an existing list called list comprehension. It is fast and efficient than for loops. It is more readable and at the same time it simplifies our code. It has the ability to represent the multiple statements containing for loop, if-else statement as well as appending modified content all in a single beautiful line of code into a new list. But that's not all! We can also use them to customize strings and apply a different set of rules on different characters. This is the true power of list comprehension.
4 |
5 | In this repo, I tried to explore all the benefits of list comprehension in the realm of for loops, conditional statements, string manipulation as well as higher-order function.
6 |
7 | Every list comprehension should follow the template as following:
8 |
my_list=[ expression for item in iterable (if condition) ]if conditionThank you! HappY Coding...
19 | -------------------------------------------------------------------------------- /list_comprehension.py: -------------------------------------------------------------------------------- 1 | # Task 1: Converting a basic for loop into a list comprehension 2 | 3 | cars = ['honda', 'toyota', 'chevrolet', 'jeep', 'ford'] 4 | 5 | # basic for loop 6 | for car in cars: 7 | print(car) 8 | 9 | # list comprehension 10 | [print(car) for car in cars] 11 | 12 | 13 | # Task 2: Converting lower case car names into uppercase 14 | 15 | new_cars = [] 16 | 17 | # using for loop 18 | for car in cars: 19 | car = car.upper() 20 | new_cars.append(car) # append method is time consuming 21 | 22 | cars = new_cars 23 | print(cars) 24 | 25 | # list comprehension 26 | cars = [car.upper() for car in cars] # no need to use append, saves time! 27 | print(cars) 28 | 29 | 30 | # Task 3: Working with Boolean values 31 | 32 | bits = [True, False, False, True, True, True, False] 33 | new_bits = [] 34 | 35 | # for loop and conditional 36 | for bit in bits: 37 | if bit == True: 38 | new_bits.append(1) 39 | else: 40 | new_bits.append(0) 41 | 42 | # list comprehension 43 | super_bits = [1 if bit == True else 0 for bit in bits] 44 | 45 | print(bits) 46 | print(new_bits) 47 | print(super_bits) 48 | 49 | 50 | # Task 4: Sring Manipulation: Customize a messy string to make it readable 51 | #eg:"ProgrammingIsFunAndExciting" => "Programming Is Fun And Exciting" 52 | 53 | my_string = "ProgrammingIsFunAndExciting" 54 | new_string = "" 55 | 56 | # loop and conditionals 57 | for char in my_string: 58 | if char.isupper(): 59 | new_string += " "+ char 60 | else: 61 | new_string += char 62 | 63 | print(new_string[1:]) 64 | 65 | # list comprehension 66 | customized_string = "".join([char if char.islower() else " " + char for char in my_string])[1:] 67 | 68 | print(customized_string) 69 | 70 | 71 | # Task 5: Use Nested For-Loops to Handle Nested Iterables 72 | 73 | genius = ['Albert', 'Newton', 'Faraday', 'Steve', 'Max'] 74 | letters = [] 75 | 76 | # loop and append 77 | for name in genius: 78 | for char in name: 79 | letters.append(char) 80 | 81 | print(letters) 82 | 83 | # list comprehension 84 | letters = [char for name in genius for char in name] 85 | print(letters) 86 | 87 | # we can add the optional if conditions after any for-loops 88 | letters = [char for name in genius for char in name if len(name) < 5] 89 | print(letters) 90 | 91 | 92 | # Task 6: Avoid higher-order function for readability 93 | 94 | # non list comprehension 95 | list1 = filter(lambda a: len(a) < 5, genius) 96 | print(list(list1)) 97 | 98 | # list comprehension way 99 | list2 = [name for name in genius if len(name) < 5] 100 | print(list2) 101 | --------------------------------------------------------------------------------