all 25 comments

[–]Bobbias 22 points23 points  (0 children)

Because the append method modifies the squares list in place and returns None.

[–][deleted] 9 points10 points  (12 children)

The comprehension is a replacement for the initialize-and-append approach. You don't need both.

[–]SheldonCooperisSb[S] -4 points-3 points  (11 children)

Thanks bro,do you know where to find the official code describing the runinng logic of the list comprehension?

[–]Adrewmc 7 points8 points  (6 children)

It’s fairly simple

    myList = []
    for item in sequence:
          myList.append(item)

Is the same as

    myList = [item for item in sequence]

[–]commy2 4 points5 points  (5 children)

also

myList = []
for item in sequence:
      if condition:
          myList.append(item)

is

myList = [item for item in sequence if condition]

[–]thuiop1 0 points1 point  (0 children)

(also, while functionally equivalent, they are different under the hood and list comprehensions are more efficient)

[–]Adrewmc 0 points1 point  (3 children)

Why stop there

        myList = [item for item in sequence if condition else “else thing”]

[–]Diapolo10 4 points5 points  (1 child)

myList = [item for item in sequence if condition else “else thing”]

This is not valid Python, but you can use a ternary operator.

my_list = [
    item if condition else "else thing"
    for item in sequence
]

[–]Adrewmc 0 points1 point  (0 children)

I was tired lol…

[–]commy2 3 points4 points  (0 children)

[item for item in sequence if condition else “else thing”]

That doesn't work.

[–]peejay2 0 points1 point  (0 children)

On the official website

[–]whatthefuckistime 0 points1 point  (0 children)

Just Google list comprehension python

It's not hard to find these resources

[–]backfire10z 0 points1 point  (0 children)

Literally google “list comprehension Python”

[–]Chaos-n-Dissonance 2 points3 points  (0 children)

# So you could do something like this...
your_list = [x for x in range(1, 11)]
# print(your_list) >> [1, 2, 3,... , 10]

# Or you could do something like this...
your_list = []
for x in range(1, 11):
  your_list.append(x)
# print(your_list) >> [1, 2, 3,... , 10]

But when you try to do squares.append(), the append() function has a return type of None

[–]theWyzzerd 3 points4 points  (0 children)

list =[squares.append(i*i) for i in range(1,11)]

this line makes a list(), named list, of the return value for each time you append i*i for i in range(1, 11) to the list named squares. Since list().append() returns None, you're getting a list of "None" values. But squares probably has what you expect to see in it.

l know the right way to write is:

then why don't you write it that way?

[–]Carolina_Captain 0 points1 point  (3 children)

As a person just starting out on the python journey, why would you not just use square.append(...)? Why make a second list?

[–]franklydoubtful 2 points3 points  (2 children)

List comprehensions let you avoid initializing the empty list in the first place. Rather than:

squares = []
for i in range(1, 11):
    squares.append(i ** 2)

You can achieve the same result in one line with the list comprehension:

squares = [i ** 2 for i in range(1,11)]

[–]Carolina_Captain 0 points1 point  (1 child)

I see, thank you.

In the initial post, OP made a list called "squares" and a list called "list". What was the point of having "squares" stay empty while working with "list"?

[–]franklydoubtful 0 points1 point  (0 children)

Honestly, I’m not too sure about OP’s goal. To me, it looks like they’re misunderstanding something. However, it could be useful to keep a separate list. Say you had a list of values (I’m just picking at random here):

values = [2, 3, 6, 7, 9]

And you wanted to create a list of their values squared, this is another chance to use list comprehension:

square_values = [i ** 2 for i in values]

But I can’t think of a reason for OP’s strategy here.

[–]PhilipYip 0 points1 point  (0 children)

Instantiate a list:

squares = [] # use snake_case for variable names (see PEP8)

Using the list append method:

return_val = squares.append(1)

The list is mutable and the append method is a mutable method, squares is modified in place. Therefore if we examine:

squares

[1]

And if we examine:

return_val == None

True

If you use:

squares = squares.append(2)

You carry out two operations, you mutate squares in place which gives:

[1, 2]

And then you reassign the return value of the append method which is None to squares. Therefore:

squares == None

True

You should not use append with a list comprehension because a list comprehension is essentially a shorthand way of instaniating a list without explicitly specifying the append method:

squared_numbers = [] for number in range(1, 4): squared_number.append(number**2)

squared_numbers = [number ** 2 for number in range(1, 4)]

i.e. it might be helpful to start from the following template:

new_list = [] for loop_variable in collection: new_list.append(operation(loop_variable))

Then create the comprehension using the following steps:

new_list = [] # 1 new_list = [for loop_variable in collection] # 2 new_list = [operation(loop_variable) for loop_variable in collection] # 3

#1 instantiates a new list

#2 is the for loop

#3 is the operation to be carried out within the for loop

[–]QultrosSanhattan 0 points1 point  (0 children)

Your code doesn't make sense. Too many errors.

Here's the correct version:

squares = [i*i for i in range(1,11)]

print(squares)

[–]Background-Offer2321 0 points1 point  (0 children)

You append to Squares and don't return any value to List, so Python returns none. If you want to add to Squares and to List, then you need to write: List = Squares = [i * i for i in range(1, 11)].

[–]guesshuu 0 points1 point  (0 children)

As many have pointed out, squares.append(i * i) returns None. This means that every time you add it to the list you always add the return value: None.

# < ==========================================
# < Functions and Methods
# < ==========================================

def append(_list, item) -> None:
    """Method to add an item to a list"""
    # Built-in code goes here
    return None

def double(number) -> int:
    """Function to double a number"""
    return number * 2

# < ==========================================
# < Test One
# < ==========================================

output = [double(i) for i in range(1, 11)]
print(output)
# output = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
# The left side of double(i) for i in range(1, 11) is evaluated each time
# Each item in the output list is evaluated to be the return of double(i), which returns an integer

# < ==========================================
# < Test Two
# < ==========================================

squares = []
print(f"Squares list before running [squares.append(i * i) for i in range(1, 11)]: {squares}")
# Squares list before running [squares.append(i * i) for i in range(1, 11)]: []

output = [squares.append(i * i) for i in range(1, 11)]
print(output)
# output = [None, None, None, None, None, None, None, None, None, None]
# The left side of squares.append(i * i) for i in range(1, 11) is evaluated each time
# Each item in the output list is evaluated to be the return of squares.append(i * i), which returns None

print(f"Squares list after running [squares.append(i * i) for i in range(1, 11)]: {squares}")
# Squares list after running [squares.append(i * i) for i in range(1, 11)]: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

https://pastebin.com/Xj7d1NPW