Home > OS >  How can I "translate" what's going on in this list comprehension?
How can I "translate" what's going on in this list comprehension?

Time:05-01

I was doing some exercises on list comprehensions and the usage of * . I think I know that it's going on, but I find it very difficult to put into concrete words. How would you explain b?

I'm not sure how to understand either what the empty string does or what the (7-i) causes in b.

a = [1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1]
b = [' ' * 2 * (7 - i)   'very' * i for i in a]
for line in b:
    print(line)

Thank you very much!

CodePudding user response:

You can replace the list comprehension with a for-loop:

b=[]
for i in a:
    nextline = ' ' * 2 * (7 - i)   'very' * i
    b.append(nextline)

What the comprehension is doing is essentially adding whitespace to the beginning, depending on i.

You can replace this: ' ' * 2 with this: ' ' (two spaces) and still get the same result. Essentially the (7-i) is displaying a certain number of whitespace characters for whatever i actually is.

CodePudding user response:

Let's split the statement up into its separate parts:

b = [' ' * 2 * (7 - i)   'very' * i for i in a]

The looping part is the last part:

for i in a

This only says that we should loop over the values of a, and do the rest of the statement for each entry.

' ' * 2 * (7 - i)   'very' * i

This is the actual statement being executed for each value in a. In both cases this is a string being multiplied -- which means it's being repeated the number of times it gets multiplied:

' ' * 2 * (7 - i)

This repeats a single space 2 * (7 - i) times; which means that it adds between 0 (2 * (7 - 7)) and 12 (2 * (7 - 1)) spaces in front of the string. The reason why they multiply by 2 is because the string very is four characters, so to center it you'll need length / 2 spaces in front of it.

'very' * i

This means very gets repeated i times, which will be 1, 2, 3, etc. times.

The merges the two strings, so that you get [0, 12] spaces, then very repeated n times.

When i is 1:

  • ' ' * 2 * (7 - 1) => 12 spaces
  • very * 1 => very
            very

If i is 2, the number of spaces decrease by 2 (7 - 2 is now 5), while very gets repeated one additional time (veryvery):

          veryvery

CodePudding user response:

Your list comprehension takes every element i (in this case, they are all integers) from list a and then stores the value of ' ' * 2 * (7 - i) 'very' * i as an element in the new list b.

When multiplying a string with an integer i, the string is repeated i times. So in your case, ' ' * 2 * (7 - i) means that a space (not an empty string!) is repeated 2 * (7 - i) times. For instance, for the second element i = 2, this means 2 * (7 - 2) = 10 spaces.

So the second part is 'very' * i and works the same way. For example, the second element i = 2, the string 'very' is repeated twice.

When adding two strings, they are concatenated. So your second element now has 10 spaces and twice the word 'very' --> ' veryvery'.

Do the same for every other number in a and you got

b = [
 '            very',
 '          veryvery',
 '        veryveryvery',
 '      veryveryveryvery',
 '    veryveryveryveryvery',
 '  veryveryveryveryveryvery',
 'veryveryveryveryveryveryvery',
 '  veryveryveryveryveryvery',
 '    veryveryveryveryvery',
 '      veryveryveryvery',
 '        veryveryvery',
 '          veryvery',
 '            very']

CodePudding user response:

So basically here is what is going on step by step:

b = [' ' * 2 * (7 - i)   'very' * i for i in a]

i is changing values from while going through a : so it has the value 1, then 2 and so on

For the case for example where i has the value 1, the following string is constructed : two white spaces " " " " 7-1 times (so basically 12 white spaces then we concatenate very 1 time (i times)

  • Related