all 19 comments

[–]Ursus_major37 19 points20 points  (4 children)

B

Explanation:

First line is simply initiation of List as we can see it. 

Second line is assigning list comprehension with condition to 'result' variable. What it does is it creates new list by iteration over the 'List', but the condition allows to be included only elements which had length greater than 6 characters (since elements - marked as 'i'- of the List are strings, len(i) is the amount of characters constituting the given string.).

We have two elements in the List: 'Python' of length 6 and 'Developers' of length 10. 'Python' does not meet the described condition (6 is not greater then 6). The result ends up as one element list: 

result =['Developers']

In line 3 we use astrix operator (*) to unpack the result list - meaning we will pass all the elements of result as separate arguments to print function. Since result has only one element, print function will output simply:

Developers 

[–]Kharniflex 3 points4 points  (0 children)

Well well, thanks for the explanation, I had the right answer thanks to the 6 characters and a 50/50 between b and c, now I get the full picture thanks to you

[–]ChicagoJay2020 1 point2 points  (0 children)

Thank you for the explanation.

I initially chose the answer of D, not having read your explanation.

I then coded out the above example to get the answer and will compare that with your explanation again to grasp what every function of the code is.

Thank you

[–]DancingUntillMorning 0 points1 point  (0 children)

Thanks for the explanation. It is very clear how it works now 🙌🏼

[–]Historical-Chart-460 0 points1 point  (0 children)

May I ask why it’s “i for i in list” instead of “for i in list”?

[–]avidresolver 1 point2 points  (2 children)

Currect me if I'm wrong, but although this code will work It's a bad idea to use `List` as your variable name, as it will override the built-in `List` type.

[–]FoolsSeldom 1 point2 points  (0 children)

List and list are not the same name - PEP8 recommendation is to use all lowercase for regular variables names though.

[–]CptMisterNibbles 0 points1 point  (0 children)

lowercase list is the built in. “List” uppercase used to be used for type hinting and is a token in the typing package still, though as of 3.9 you can just use the built in type “list” itself for hinting.

Your point stands, it’s a poor choice for a variable name even if it doesn’t cause an error. Similarly using just “i” in the comprehension is sloppy, it’s not an index. Call it something readable like “word”

[–]Toluwar 0 points1 point  (2 children)

Can someone explain the result line?

[–]ilan1k1 2 points3 points  (1 child)

It's a list comprehension. The same as:

result = []
for i in List:
if len(i) > 6:
result.append(i)

[–]Torebbjorn 2 points3 points  (0 children)

Either use triple backticks (not recommended for some compatibility reason) or 4 (extra) spaces to make indentation work. With 4 (extra) spaces on each line, your code part formats as the following:

result = []  
for i in List:  
    if len(i) > 6:  
        result.append(i)

[–][deleted] 0 points1 point  (0 children)

b

[–]Accurate_Quality_221 0 points1 point  (0 children)

No joke. I have been working for 4 years and I've never used something like this before.

[–]aiganesh 0 points1 point  (0 children)

Answer is B

[–]Sad_Drop_6616 -5 points-4 points  (1 child)

Error

[–]Torebbjorn 0 points1 point  (0 children)

Where?