1

In my Python program, I was told that this code snippet:

listOfNumbers = input() # (This inputs a space separated list of integers)
listOfNumbers = listOfNumbers.split()

for x in range(0, len(listOfNumbers)):
    listOfNumbers[x] = int(x)

Can be replaced by this one-liner:

listOfValues = [int(x) for x in input().split()]

I am new to Python so I don't know many code-shortening techniques or methods, so I started to analyse the snippet given.

I am confused about why part of the line is wrapped in square brackets: [int(x) for x in input().split()]

What do the [ and ] at the start and end of that section of code do?

Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
  • 3
    The term to google is "list comprehension". – Fred Larson May 22 '20 at 18:21
  • 1
    It indicates to python we are iterating and appending values to a list. – Red May 22 '20 at 18:21
  • 1
    It's a list comprehension. It creates a list using the inner for loop. – Xbel May 22 '20 at 18:22
  • 1
    Read [explanation-of-how-nested-list-comprehension-works](https://stackoverflow.com/questions/20639180/explanation-of-how-nested-list-comprehension-works) for some more complex list comprehensions. Also read the [documentation](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions) – Patrick Artner May 22 '20 at 18:23
  • 1
    This is not True. Your first code does not change the content of the list at all - the `x=int(x)` only works for that specific `x` inside the loop when you are there. NOT modifying the original list. – Patrick Artner May 22 '20 at 18:25
  • Another way to tackle what the list comp does, would be `listOfValues = list(map(int, input.split()))` – Patrick Artner May 22 '20 at 18:26
  • @Ary you made it worse. x is a string, you cannot index a list by a string. – Patrick Artner May 22 '20 at 18:29
  • @PatrickArtner `listOfValues = list(map(int, input.split()))` doesn't work: `AttributeError: 'builtin_function_or_method' object has no attribute 'split'` – Aryan Beezadhur May 22 '20 at 19:02
  • `listOfValues = list(map(int, input().split()))` – Patrick Artner May 22 '20 at 19:12

2 Answers2

2

It's what is known as a list comprehension.

Consider you want to make the first 5 numbers of the number line into a list.

The complete block of code would be written as:

myList = []
for i in range(1, 6):
    myList.append(i)

This can be shortened in a comprehension into:

myList = [i for i in range(1, 6)]
myList
>>> [1, 2, 3, 4, 5]

It has other applications but it can be used to make a list in a fewer set of lines as above.

The square brackets, as you can see also exist in the main list, so it highlights the fact that a list is being made.

This can be seen more clearly if you wish to make a dictionary.

myDict = {key: 0 for key in range(1, 6)}
myDict
>>> {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}
vik1245
  • 546
  • 2
  • 9
  • 26
2

List comprehension is a syntactic sugar in python (that is faster). The [ and ] are list creators:

x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

List comprehension simplifies that to this:

x = [x for x in range(0, 10 + 1)]
Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
xilpex
  • 3,097
  • 2
  • 14
  • 45