Skip to content

Instantly share code, notes, and snippets.

View PhoenixIM's full-sized avatar

Iffat Malik PhoenixIM

  • United Kingdom
View GitHub Profile
@PhoenixIM
PhoenixIM / set_comprehension.py
Created July 26, 2020 09:37
set comprehenstion
#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']]
@PhoenixIM
PhoenixIM / lc_with_nested_floop2.py
Created July 26, 2020 09:35
list comprehension with nested for loop
#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)):
@PhoenixIM
PhoenixIM / lc_with_nested_floop1.py
Created July 26, 2020 09:33
list comprehension with nested for loop
#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:
@PhoenixIM
PhoenixIM / lc_with_ifelse2.py
Created July 26, 2020 09:30
list comprehension with if-else statement
#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:
@PhoenixIM
PhoenixIM / lc_with_ifelse1.py
Created July 26, 2020 09:28
list comprehension with if-else
#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)
@PhoenixIM
PhoenixIM / lc_with_if2.py
Created July 26, 2020 09:26
list comprehension with if statement
#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)
@PhoenixIM
PhoenixIM / lc_with_if.py
Created July 26, 2020 09:24
list comprehension with if statement
#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)
@PhoenixIM
PhoenixIM / lc_with_for_loop.py
Created July 26, 2020 09:21
list comprehension with for loop
#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}")
@PhoenixIM
PhoenixIM / list4.py
Created July 26, 2020 09:17
creating a list using map()
#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}")
8
64
216
512
1000