-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathComprehensions.py
More file actions
28 lines (24 loc) · 1.06 KB
/
Comprehensions.py
File metadata and controls
28 lines (24 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# comprehensions are concise way to create lists, sets , dictionarys , generators in Python using a single line of code
# they are use in filtering , transferming item , create a new collection
# cleaner code => faster execution
# List => syntax [expression for item in iteratable if condition]
car = ['toyota' , 'Honda' , 'BMW' , "BYD" , 'BMW']
# user = input('Enter car name')
car_result = [ eachItemCar for eachItemCar in car if eachItemCar == 'BMW' ] #single line
print(car_result)
# set => as we know set have unique values thar did'n repeate syntaz {expression for item in iteratable if condition}
unique_car = {eachCarValue for eachCarValue in car if len(eachCarValue) }
print(unique_car)
show_Room = {
"Honda" : ['civic' , "City" , "City"],
"byd" : ['gass' , "petrol" , "Electrics"]
}
my_car = { eachCar for car in show_Room.values() for eachCar in car }
print(my_car)
# Dictionary =>
car_price = {
"Honda" : 5000,
"byd" : 4000,
}
car_price_discount = { brand:brand_price for brand , brand_price in car_price.items() }
print(car_price_discount)