Python Collections

The objective of this project is to practice your Python list handling skills, including: adding and removing elements, combining lists, using membership operations and, most importantly, iterating them. We'll use a dataset containing a nested list of students data.

All our Data Science projects include bite-sized activities to test your knowledge and practice in an environment with constant feedback.

All our activities include solutions with explanations on how they work and why we chose them.

codevalidated

Assume that you are compiling a list of mathematical constants, and you decide to represent all of them in a Python list, following this structure:

```
maths_constants = [['name of constant', 'numeric value'],
['name of constant', 'numeric value'],
['name of constant', 'numeric value'],
...
]
```

As exemplified in the list `maths_constants`

. However, upon review, you realize that you missed the value of `Pi`

.

Let's address this by first identifying its prospective index if it were included in the list, considering the alternating pattern of string constants and their numeric values.

Store the index in the variable `index_pi`

.

codevalidated

As you have found the index of the `Pi`

, now you have to insert the value after the name `Pi`

in the list.

Your final complete list for mathematical constants including the value of Pi should be in the variable `maths_constants`

.

Input the value of Pi as

`3.1416`

(in float)

codevalidated

Add all the characters of our `even_string`

to the end of our list `odd_list`

.

**[!] Important**:
1. Do Not Use Loops and if else statements.
2. Add each character of the string, not the whole string.

After extending, the final result will be: `[99, 101, 103, 105, 107, 109, '2', '4', '6', '8', '9']`

codevalidated

As our `odd_list = [99, 101, 103, 105, 107, 109, '2', '4', '6', '8', '9']`

, we need to remove '9'.

input

The composition of our dataset `student_list`

:

```
[['name', 'student_id', 'age','grade','subject'],
['name', 'student_id', 'age','grade','subject'],
[...],...]
```

input

Your task is to identify the name of the 400th student in our dataset. Each element in the dataset is a list of five items:

```
[
['name', 'student_id', 'age', 'grade', 'subject'],
['name', 'student_id', 'age', 'grade', 'subject'],
... among others...
]
```

Example of input: `Alex`

multiplechoice

Select the correct age and subject of our student at 273rd index.

codevalidated

Create a list `select_student`

and store the students from the index 98 to 214 into it.

Make sure to not change the order of the students.

codevalidated

From previous activity we have the list `select_student`

which contains the students from the index 98 to 214. Now we need to find the `name, age, grade, subject`

of the last student from our `select_student`

list. Store the values in the variables `name, age, grade, subject`

respectively.

codevalidated

Convert the ages into

`int`

as currently they are type`str`

(string).

Loop through the list `student_list`

, access the age, cast it to int and append it to the new list variable `students_age`

.

input

Use the list `students_age`

created in the previous activity and find the average age of the students.

Average/mean Age is defined as sum of age of all the students divided by the number of students.

codevalidated

Iterate through the list `student_list`

and check for each of these subjects, incrementing each time the variable for the corresponding subject and store the result in the variables `history_lovers`

, `english_lovers`

and `maths_lovers`

respectively.

We assume that, in the list

`student_list`

, any particular student can have multiple subjects and also have multiple grades in same subject. Hence, we count that student only once. For example, if a student has`History`

as one of the subjects and has`A`

grade in`History`

as well as`B`

grade in`History`

, we count that student only once.

codevalidated

Create two lists `a_graders`

and `b_graders`

with the name, age, grade and favorite subject of the students. Put students with `A`

grades to the list `a_graders`

and students with `B`

graders to the list `b_graders`

Expected output for `a_graders`

:

```
[['Luna', '18', 'A', 'English'],
['Anthony', '18', 'A', 'Math'],
['Bella', '17', 'A', 'History'],
['Lucy', '17', 'A', 'History'],
['Brooklyn', '18', 'A', 'History'],
['Ava', '18', 'A', 'Art'],
['Riley', '17', 'A', 'Math'],
...
]
```

input

Find the grade of student with name `Isaac`

and age `16`

and subject `Science`

.

Enter your answer in the input box below.

multiplechoice

How many students are there whose name starts with the letter `S`

in the list `student_list`

?

There are multiple students with the same name in the list

`student_list`

. You should count each student only once. Use student id to identify different students.

multiplechoice

How many students are under 17 in the list `student_list`

?

There are multiple students with the same name in the list

`student_list`

. You should count each student only once. Use student id to identify different students.

input

Input Example: `Min: 20, Max: 29`

.

codevalidated

Remove the student whose name is `Luna`

, age is `18`

, grade is `E`

and subject is `Geography`

from the list `student_list`

. If there are multiple students with the same name, age, grade and subject, remove all of them.

input

Count the number of occurrence of the student whose name is `Luna`

and age is `16`

in the list `student_list`

. If there are multiple students with the same name and age, count all of them.

Author

This project is part of

Explore other projects