3

I don't understand the syntax for list comprehension:

newList = [expression(element) for element in oldList if condition]

The bit I don't understand is (element). Let's say you had a following code:

List = [character for character in 'Hello world!']
print(list)

And then you will get:

['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']

Since the first character isn't quite an expression, what is it doing? Does it just mean that each item in the string is getting stored in a new list?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • You can look at [Python List Comprehension, How To?](https://stackoverflow.com/questions/19104760/list-comprehension-in-python-how-to) to understand how the comprehension works. – ThePyGuy Jul 16 '21 at 11:19
  • But `character` *is* an expression. And, yes, a list comprehension *is* creating a new list. – quamrana Jul 16 '21 at 11:21
  • you shouldn't use `List` as a variable name as it is a reserved keyword try using my_list to avoid probelms – TERMINATOR Jul 16 '21 at 11:58
  • Does this answer your question? [What does "list comprehension" and similar mean? How does it work and how can I use it?](https://stackoverflow.com/questions/34835951/what-does-list-comprehension-and-similar-mean-how-does-it-work-and-how-can-i) – Gino Mempin Oct 13 '22 at 10:24

2 Answers2

2

Python list comprehensions are for loops executed in a list to generate a new list.The reason python list comprehensions are evaluated backward from or right to left is because usually anything inside a bracket( [], {}, () ) in python is executed from right to left with just a few exeptions .Another thing to note is that a string is an iterable (lists,tuples, sets, dictionaries, numpy arrays) concatenating characters so it can be iterated over like a list.

List Comprhension form:

new_list = [item for item in my_list]

This will have the same effect:

for item in my_list:
    my_list.append(item)

Since a strings is an iterable of characters you can do this:

my_list = [character for character in 'Hello world!'] 
print(list)

Output:

['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']

Your list comprehension can also be written as:

my_list = []
for character in 'Hello world':
    my_list.append(character)

print(my_list)

I am also pointing out that you shouldn't use built in methods(such as List) as variable names because when you do you overide them which will bar you from using that method in the future.

Here is a complete list of all builins as of python 3.9.6:

Built in methods

TERMINATOR
  • 1,180
  • 1
  • 11
  • 24
1

Let's understand the syntax for Python List Comprehensions using a few examples. Reference to complete documentation.

Basic usage: [<expression> for <item> in <iterable>]. In python, any object that implements the __next__() method is an iterable.

For example, List objects are iterables. Consider the following code:

list_a = [1, 2, 3]
list_b = [item for item in list_a]     # [1, 2, 3]
list_c = [item + 1 for item in list_a] # [2, 3, 4]
list_d = [None for item in list_a]     # [None, None, None]

Interestingly, a String object is also iterable. So when you iterate over a string, each item will be a character. This was the case in your example.

Now, coming to expressions. In python, any value (integer, float, char, etc) or any object is an expression. Just to clarify, the expression does not contain an equals symbol. This is an excellent answer to the question, "What is an expression in Python?".

I noticed that you had used (also pointed out in comments), list as the name of a variable. There are some keywords and names you should not use as variable names in Python. You can find the list of all such builtin names by: (refer this post for more details)

import builtins
dir(builtins)
Swaroop
  • 1,219
  • 3
  • 16
  • 32