List Comprehension (i = i -1 or i = i + 1)

While taking the quiz I was asked Which of the list comprehension equal to desired_list

desired_list = [-1,0,1,2,3]

Both of these produce the correct answer
[i - 1 for i in range(5)]

desired_list = []
for i in range(5)
desired_list = i - 1

I understand its used to count up or down but I don’t understand its use in this context. The elements in the list goes up by 1. So why are we going down by 1?

I’m not so sure about the second one but as for the usage have you tried using a different change in i and examining the output. What is range being used for in this case? It might be worth taking the time to understand list comprehension syntax now whilst it’s still fresh. If necessary create a working for loop version first and then making your list comprehension so you work out which bits go where.

1 Like

Well, both of them are correct. You just need to look at each iteration.

  • 1st iteration: i = 0 and we require: -1 so, we will perform: i - 1 [0 - 1 = -1]

  • 2nd iteration: i = 1 and we require: 0 so, we will perform: i - 1 [1 - 1 = 0]

  • 3rd iteration: i = 2 and we require: 1 so, we will perform: i - 1 [2 - 1 = 1]

… so on till: i = 4.

I believe, this satisfies your query.


1 Like

Are they? I’d have a quick copy/paste test.

1 Like

Range just makes sure the list has a certain amount of elements so we will gave 5 elements in the list list.

If it was just
i for i in range(5) it would start from 0 and end with 4
i + 1 for i in range(5`) it would start from 1 and end with 5
i - 1 for i in range(5) it would start from -1 and end with 3

All of these lists have five elements but end and start with different numbers.

Since the index starts at 0 could we also say
0 + 0 = 0
0 + 1 = 1
0 - 1 = -1
based off of the i - 1 and i + 1. And to be honest, if it was i by itself it would just get left alone and the index will stay at one
So when it comes to ranges and loops the i + 1 and i - 1 just mean where the index will start.
Sorry if I am dragging it on I am just typing it out to make sense of my thoughts and see if I really understand.

Making sure you understand it should be your primary goal so I wouldn’t worry about dragging it on at all. Just so long as you’re still trying to grasp the concept.

I wouldn’t say that using range(5) has the primary purpose of ensuring a set number of elements. After all so would using range(-10, -5). It’s also not really being used for the sake on an index. It is what is providing us with the useful values.

What about-

# A five element list-
print([x for x in range(-10, -5)])
# A five element list with a different output-
print([x - 1 for x in range(-10, -5)])

Think of values instead? Ok, I see. Index helps me break it down and understand why it started and ended with those values. The index started with -10 and when we did x-1 it started with -11 instead. I’ll have to do more practices to see where and how it’s used. But for now, it seems like we use it to make sure we get the output/value we want.

Range is just a fast, convient and low cost way to create a range of values. We could use an actual sequence , e.g. [x * 2 for x in [0, 1, 2, 3, 4, 5]] but we’re in the business of getting the computer to do all the hard work. Focus on the value rather than an index per se, e.g.

print(['t' + word for word in ["ic", "ac", "oe"]])

Though the order is, of course, also important (and consequently so is the ‘index’).

1 Like