Day 23 was about lists, dictionaries and creating lists from lists and dicts from dicts
Done with Day 23 of my #100DaysOfCode #Python challenge today was about list and dictionary comprehensions and making your code just a bit shorter using it.
— Wouter Kursten (@Magneet_nl) January 26, 2021
lists and list comprehensions
create new list from list
new_list = [new_item for item in list]
numbers = [1, 2, 3, 4
newer_list = [item+1 for item in numbers]
print(newer_list)
can also use if
new_list = [n*2 for n in range(1,17) if n % 2 == 0]
dictionary comprehensions
new_dict = {new_key:value for item in list}
new_dict = {new_key:value for (key,value) in dict.items()}
treat panda dataframe as dict
import pandas
student_dict ={
"student": ["Miep", "Wouter", "Henk"],
"score": [56,76,23]
}
student_panda_fram = pandas.DataFrame(student_dict)
for (index, row) in student_panda_fram.iterrows():
print(row.student)
