-3

I am have read python documentations regarding list comprehensions I got an idea about list comprehensions work flow, but unable to understand the process involved in this code.

items=[x for x in raw_input().split(',')]
Ravy
  • 89
  • 1
  • 1
  • 8

1 Answers1

1

It creates a list containing each word from input (from terminal) separated by comma ,.

Edit:

items = [x for x in raw_input().split(',')]
print items

with input from terminal:

this, is, a, string

prints:

['this', ' is', ' a', ' string']

Edit2:

As pointed out in the comments, the list comprehension is redundant, and you would achieve the same using

items = raw_input().split(',')

Edit3:

Also mentioned in the comments, the above approach only works in python version 2. For python3, you'd use

items = input().split(',')                                                      
print (items)
Plasma
  • 1,903
  • 1
  • 22
  • 37