-7

I'm studying Python 3. Recently I've started to use HackerHank, and there I found a solution to a certain challenge that I had not completed. However, I didn't understand some of the lines of code.

This is the code:

import statistics;
n = int(input())
x = list(map(int, input().split()))
x = sorted(x);
median = statistics.median(x)
L = (i for i in x if i < median)
U = (i for i in x if i > median)

These are the two lines that are confusing me:

L = (i for i in x if i < median)
U = (i for i in x if i > median)
Christian Dean
  • 22,138
  • 7
  • 54
  • 87
  • Should be easy to find in a Python tutorial: – duffymo Mar 05 '18 at 20:45
  • 2
    There called _generator expressions_. If you google the name, some good articles should come up. – Christian Dean Mar 05 '18 at 20:46
  • 1
    Pedro, haven't downvoted your question but I suggest you first try to tweak a little bit your question so it becomes a [mcve](https://stackoverflow.com/help/mcve), that way people will be able to test your code and you'll definitely get more answers – BPL Mar 05 '18 at 20:46

1 Answers1

0
L = (i for i in x if i < median)
U = (i for i in x if i > median)

is code for checking if elements i of a list x are lower or higher than a median value

with for i in x the list is parsed, if i < median L ( lower value ) is set to i or resp if i > median U ( upper value ) is set to i

also see Generator Expressions vs. List Comprehension

ralf htp
  • 9,149
  • 4
  • 22
  • 34