This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Set Comprehension | |
| #dictionary inside a list | |
| person = [ {'name': "Emma", 'language': ["Python", "Java"]}, | |
| {'name': "Harry", 'language': ["C++", "Python"]}, | |
| {'name': "Lily", 'language': ["Python"]} | |
| ] | |
| #with List Comprehension | |
| person_lc=[l for p in range(len(person)) for l in person[p]['language']] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #4.List Comprehension with nested for loop | |
| #Part-2 | |
| #dictionary inside a list | |
| person = [ {'name': "Emma", 'language': ["Python", "Java"]}, | |
| {'name': "Harry", 'language': ["C++", "C#"]} | |
| ] | |
| language=[] | |
| for p in range(len(person)): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #4.List Comprehension with nested for loop | |
| #Part-1 | |
| #cartesian product of two Lists | |
| char_list=['a','b','c'] | |
| int_list=[0,1,2,3] | |
| cartesian=[] | |
| for x in char_list: | |
| for y in int_list: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #3.List Comprehension with if-else | |
| #Part-2 | |
| #Creating a List of two Lists - one for even numbers another for odd numbers.(Lists within a List) | |
| numbers=[] | |
| even=[] | |
| odd=[] | |
| for num in range(10): | |
| if num%2==0: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #3.List Comprehension with if-else | |
| #Part-1 | |
| square_cube=[] #creating a List of numbers - even number->square and odd number->cube | |
| for num in range(10): | |
| if num%2==0: | |
| square_cube.append(num**2) | |
| else: | |
| square_cube.append(num**3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #2.List Comprehension with if. | |
| #Part-2 | |
| #list out words starting from letter 'P' | |
| statement="List Comprehension is one of the famous features of Python Programming" | |
| words=[] | |
| for w in statement.split(): | |
| if w.startswith('P'): | |
| words.append(w) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #2.List Comprehension with if. | |
| #Part-1 | |
| #creating a List of even numbers' cube | |
| num_cube_floop=[] | |
| for n in range(1,11): | |
| if n%2 == 0: | |
| num_cube_floop.append(n**3) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #1.List Comprehension with for loop | |
| #print the characters of a string as List | |
| name="Python Programming" | |
| name_ltr=[] | |
| for letter in name: | |
| name_ltr.append(letter) | |
| print(f"name_ltr[] = {name_ltr}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #4.Using map(). | |
| numbers=[2,4,6,8,10] | |
| num_cube_map=list(map(lambda n:n**3,numbers)) | |
| print(f"num_cube_map[] = {num_cube_map}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 8 | |
| 64 | |
| 216 | |
| 512 | |
| 1000 |