Did I Misunderstand My Python Assignment?

0
6
Asked By CuriousCoder42 On

Hey everyone, I'm currently going through a Python course, and I've hit a bit of a wall with one of the assignments. The task is to create a function called `odd_indices()` that takes a list of numbers as input and returns a new list containing values from the original list at every odd index. They provided a detailed step-by-step guide on how to achieve this, which involves defining the function, creating a new list, using a loop to append elements at odd indices, and eventually returning that new list.

Here's my solution:
```python
def odd_indices(my_list):
return my_list[1:len(my_list):2]
```

And here's what they suggested:
```python
def odd_indices(my_list):
new_list = []
for index in range(1, len(my_list), 2):
new_list.append(my_list[index])
return new_list
```

I feel like my approach is simpler and works just fine, but I can't shake the feeling that I might have missed the point of the exercise. It seems excessive to loop through the list when slicing accomplishes the same thing. Am I overthinking this? Did I fail in following their instructions?

3 Answers

Answered By CodeCraftsman123 On

There are definitely multiple ways to solve a problem, so don’t be too hard on yourself! Your version is cleaner, but the course likely wants you to build foundational skills. They might think that grasping loops will help you as you advance. So it's less about right or wrong and more about understanding and learning!

Answered By ListLover! On

Your answer is fine, but the fact that the assignment explicitly mentioned loops multiple times suggests they wanted you to use them for a reason. It's part of learning the fundamentals, and even though your solution is shorter, it's good to grasp how to iterate and manipulate lists through loops. Consider it practice more than failure!

Answered By PythonNinja99 On

I wouldn’t say you’ve failed! Your solution works perfectly, but the assignment seems to emphasize learning through using loops, which is a fundamental concept in programming. The course might be focusing on teaching you how iteration works in relation to lists, rather than just making it easier with slicing. Your approach is definitely valid but keep in mind that following the specific instructions is important in these learning scenarios.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.