1

I am a novice in python, and got this code:

nlist = autoscaling_connection.get_all_launch_configurations()
name = "CONFIG"    
versions = [x.name[len(name):] for x in nlist
                if (x.name.startswith(name) and is_number(x.name[len(name):]))]

I understand that the for loop is concatenated into the versions list but I do not understand the assignment:

[x.name[len(name):]
Alex
  • 7,007
  • 18
  • 69
  • 114
  • What is `name`? It's defined externally. What about `nlist`? – Mad Physicist Sep 15 '18 at 20:50
  • @MadPhysicist that doesn't matter here too much, as he is simply asking about the syntax of the list comprehension. (although it would if he was trying to provide a MVCE) :) – dangee1705 Sep 15 '18 at 20:51
  • Sure. I'm promoting him to ask clearer questions, but not insisting on it. – Mad Physicist Sep 15 '18 at 20:54
  • Possible duplicate of [Understanding Python's slice notation](https://stackoverflow.com/questions/509211/understanding-pythons-slice-notation) – Patrick Artner Sep 15 '18 at 20:55
  • added the definition of nlist and name – Alex Sep 15 '18 at 21:04
  • It's doing a search by name like e.g. `max` and expects the format `max1234` for items in `nlist`. Eventually you will have a list (`versions`) only containing `'1234'` (sticking to my example). So if `nlist` also contained `max4321`, `versions` would look like this: `['1234', '4321']`. – rocksteady Sep 15 '18 at 21:07
  • Hi @Alex, is there anything else you would like explaining? (you haven't accepted an answer I assume you forgot or would like something else explaining) – dangee1705 Sep 15 '18 at 21:25

3 Answers3

3

I'll expand the whole code, then go through it

versions = [x.name[len(name):] for x in nlist
            if (x.name.startswith(name) and is_number(x.name[len(name):]))]

is equivilent to

versions = []
for x in nlist:
    if (x.name.startswith(name) and is_number(x.name[len(name):])):
         versions.append(x.name[len(name):])

as for what

x.name[len(name):]

means, it means to take from x.name, the characters starting at the index which is equal to len(name) (the length of name), to the end of the x.name

so in a simpler example, say I have a list

my_list = [1, 2, 3, 4, 5]

and I do

my_list[2:]

then this would return

[3, 4, 5]

which is the same as from the 2th element to the end.

I hope this helps. If you want to learn more about python slice notation, I can recommend this other SO question: Understanding Python's slice notation

dangee1705
  • 3,445
  • 1
  • 21
  • 40
0

In python if x is a list and i is an index (an integer).

x[i:] 

means "all item in x starting at index i all the way to the end"

You can also have

x[i:j]

"all item in x starting at index i all the way to, but not including j"

x[:j]

"all item in x starting at beginning all the way to, but not including j"

y = x[:]

"copy all elements of x in a new list and assign it to y"

y = x[2:9:3]

"every third element of x starting at index 2 and ending before index 9."

There are more I'd suggest looking up list properties in Python.

Michael Ekoka
  • 19,050
  • 12
  • 78
  • 79
0

A slightly more complete (but guesstimated) code:

class K:
    """A class that has a name property"""
    def __init__(self,name):
        self.name = name

# a prefix to look for            
name = "Fiat "


# a list of objects that have a name - some starting with our name
nlist = [K("Fiat 2"), K("Fiat 4"), K("Fiat 2711999"), K("Fiat Dupe 09"), K("DosntFly 0815")]

def is_number(text):
    """Test if the text is an integer."""
    try:
        int(text.strip())
    except:
        return False
    return True


versions = [x.name[len(name):] for x in nlist
                if (x.name.startswith(name) and is_number(x.name[len(name):]))]

print(versions)

Output:

['2', '4', '2711999']

Why?

The list comprehension test each element of nlist.

It will let only things into versions that statisfy the if (x.name.startswith(name) and is_number(x.name[len(name):].

Meaning: each classes instanse name has to start with the variable name's content and whatever comes after as many characters as name got, has to check as True if fed to is_number().

If this fits, it is taking the "number-part" and adds it as element into the resulting list.

"Fiat Dupe 09"  # not in the list, because "Dupe 09" is not True
"DosntFly 0815" # not in the list, because not starting with "Fiat "
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69