C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python List ComprehensionList Comprehension is defined as an elegant way to define, create a list in Python and consists of brackets that contains an expression followed by for clause. It is efficient in both computationally and in terms of coding space and time. SignatureThe list comprehension starts with '[' and ']'. [ expression for item in list if conditional ] Exampleletters = [] for letter in 'Python': letters.append(letter) print(letters) Output: ['P', 'y', 't', 'h', 'o', 'n'] Exampleletters = [ letter for letter in 'Python' ] print( letters) Output: ['P', 'y', 't', 'h', 'o', 'n'] Examplex = {'chrome': 'browser', 'Windows': 'OS', 'C': 'language'} x['mouse'] = 'hardware' print(x['Windows']) Output: OS
Next TopicPython Tutorial
|