Skip to content

Instantly share code, notes, and snippets.

@rajivnarayana
Created January 30, 2021 09:52
Show Gist options
  • Save rajivnarayana/7c4245f79c0b3d1ff849d12fc565ba7d to your computer and use it in GitHub Desktop.
Save rajivnarayana/7c4245f79c0b3d1ff849d12fc565ba7d to your computer and use it in GitHub Desktop.
Data transformation in python.
# Online Python compiler (interpreter) to run Python online.
# Write Python 3 code in this online editor and run it.
students = [
{"name": "user1", "marks": 10, "gender": "male"},
{"name": "user2", "marks": 12, "gender": "male"},
{"name": "user3", "marks": 15, "gender": "male"},
{"name": "user4", "marks": 20, "gender": "female"},
{"name": "user5", "marks": 25, "gender": "female"}]
print("1: print all student names")
for student in students:
print(student["name"])
print("2: Print average marks of female students (using lambda)")
sum = 0;
count = 0;
for student in filter(lambda x : x["gender"] == "female", students):
sum+= student["marks"]
count += 1
print("Sum of all female student marks: "+ str(sum));
print("Average "+ str(sum/count));
print("2: Print average marks of female students (without using lambda)")
sum = 0;
count = 0;
filteredStudents = [];
for student in students:
if student["gender"] == "female":
filteredStudents.append(student);
print (filteredStudents);
//https://gist.github.com/rajivnarayana/a5eebae8c6b4d666a0dbd4eacba72b82
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment