Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

list comprehension python

friends = [
    {'name': 'Sam', 'gender': 'male', 'sport': 'Basketball'},
    {'name': 'Emily', 'gender': 'female', 'sport': 'Volleyball'}
]
# filter by column names
print([{key: friend[key] for key in friend.keys() if key in ["name", "sport"]} for friend in friends])
# filter by rows
print([a for a in friends if a["name"] in ["Sam"]])
Source by courses.bootcampspot.com #
 
PREVIOUS NEXT
Tagged: #list #comprehension #python
ADD COMMENT
Topic
Name
8+5 =