Python: Why do these lines of code display different values?

Viewed 58

I am relatively new to coding and python and I am trying to wrap my head round some concepts. One I am struggling on is the split() function and why these two pieces of code produce two different outputs

y = ["hello\n", "world\n", "python\n"]
x = [line.strip() for line in y]
print(x)

and

y = ["hello\n", "world\n", "python\n"]
for line in y:
    x = [line.strip()]
print(x)

The first piece of code produces an output of

['hello', 'world', 'python']

Whilst the second produces

['python']

Does anyone know why this is, as to me they should do the same thing by both producing an output of

['python']

thanks

5 Answers

In the second one you overwrite x for each loop iteration, and you only see x's value from the last iteration.

Does anyone know why this is, as to me they should do the same thing by both producing an output of ['python'].

Your first code uses list comprehension which is used to create a sequence. It provides a concise way to create lists or sequence of elements.

Your second code is basically using a for loop just to get the last element of list y. for loop is used if you want to iterate through a sequence. However, you are also overwriting the variable x in each of your iteration and hence, when you come out of for loop, it will be pointing to the last element (i.e., ['python']). Try this:

for line in y:
    x = [line.strip()]
    print(x)

If you just want the last element of your list y and strip() it, you can also do that via:

x = [ y[-1].strip() ]

OR

If you want the list, then use your first code.

MORE INFORMATION

If you can change your second code to:

x = [ ]
y = ["hello\n", "world\n", "python\n"]
for line in y:
    x.append(line.strip())
print(x)

Then both will print same output (i.e., ['hello', 'world', 'python']).

The first version of your code is using a list comprehension, where you accumulate the results of processing each element in an output list that contains all the elements.

Whereas in the second version, you process one element at a time inside the loop, saving the result in a single-element list and overwriting the previous value, so when you exit the loop the variable will contain the last element that was processed.

x = [line.strip()] creates a new list with a single element each time you iterate through the loop. You can more easily see what is happening by moving the print() inside the loop:

y = ["hello\n", "world\n", "python\n"]
for line in y:
    x = [line.strip()]
    print(x)

in your first case:

y = ["hello\n", "world\n", "python\n"]
x = [line.strip() for line in y]
print(x)

you are using a list comprehension to define your variable x by iterating over all the elements from y and keeping them into a new list, this line is equivalent with:

x = []
for line in y:
    x.append(line.strip)

in your second case:

y = ["hello\n", "world\n", "python\n"]
for line in y:
    x = [line.strip()]
print(x)

you are assigning to the same variable x a different value on each iteration of the for loop this means that your variable x will end to have the last value from your list y (with str.strip applied)


another nice way to achieve your desired output:

list(map(str.strip, y))
Related