jeudi 25 août 2016

True/False in lists [duplicate]

This question already has an answer here:

I stumbled upon a nifty tool in Python, but I can't seem to find what it's called in the community. I've tried searching through list comprehension pages, but nothing I've found mentions this. Here is an example code:

w = 'abc'

x = ['q', 'k'][len(w) == 3]
y = ['q', 'k'][len(w) > 4]

print x
print y

And the output will look like:

k
q

So there's a list, followed by a true/false block. If the code in the block is True, it will print the second item in the list; if False, it will print the first item.

I wanted to learn more about this type of structure, but like I said, I can't seem to find any leads. I was thus wondering if someone could point me in the right direction. And, if you have any information to add, please share!

Thank you!

Aucun commentaire:

Enregistrer un commentaire