-1

I have a list with 22 integers (ranging from 1 through 9) and want to create/ print a new list containing only those integers that are above 5.

This is what I have tried so far - the result (obviously) is that 'the_list' gets printed multiple times - i.e. the number of times = the number of instances above 5.

the_list = [1, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7]
print(the_list)

k=5
tl2=[]
for i in the_list:
    if i > k :
        tl2.append(the_list)
Cloudomation
  • 1,597
  • 1
  • 6
  • 15
tristar8
  • 75
  • 7

3 Answers3

2

Try this code:

>>> the_list = [1, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7]
>>> print(the_list)
[1, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7]
>>> the_filtered_list = list(filter(lambda x: x > 5, the_list))
>>> print(the_filtered_list)
[7, 6, 6, 7, 6, 6, 7]

See

EDIT:

Another option is to use a generator expression:

>>> the_filtered_list = list(i for i in the_list if i > 5)
>>> print(the_filtered_list)
[7, 6, 6, 7, 6, 6, 7]

See


EDIT:

My initial answer was indeed slow and memory inefficient. Here is the comparison of several possibilities. Which one to choose depends on how big the list is and what it is used for later.

>>> import random
>>> import timeit
>>> import sys
>>> 
>>> the_list = [random.randrange(1, 10) for _ in range(100)]
>>> 
>>> timeit.timeit('filter(lambda x: x > 5, the_list)', setup=f'the_list = {the_list}')
0.15890196000000856
>>> timeit.timeit('[i for i in the_list if i > 5]', setup=f'the_list = {the_list}')
2.633208761999981
>>> timeit.timeit('(i for i in the_list if i > 5)', setup=f'the_list = {the_list}')
0.227755295999998
>>> 
>>> timeit.timeit('list(filter(lambda x: x > 5, the_list))', setup=f'the_list = {the_list}')
7.5565902380000125
>>> timeit.timeit('list(i for i in the_list if i > 5)', setup=f'the_list = {the_list}')
3.599053368
>>> 
>>> sys.getsizeof(filter(lambda x: x > 5, the_list))
64
>>> sys.getsizeof([i for i in the_list if i > 5])
440
>>> sys.getsizeof((i for i in the_list if i > 5))
128
>>> 
>>> sys.getsizeof(list(filter(lambda x: x > 5, the_list)))
480
>>> sys.getsizeof(list(i for i in the_list if i > 5))
480
Cloudomation
  • 1,597
  • 1
  • 6
  • 15
  • 1
    `list(filter(lambda...` when a comprehension is that much shorter and more readable... just short of a downvote :) – user2390182 Sep 27 '19 at 07:34
  • filter with use of lambda is much slower compared to list comprehension. `new_list = [i for i in the_list if i>5]` should be the ideal solution. – P.hunter Sep 27 '19 at 07:40
  • why `the_filtered_list = list(i for i in the_list if i > 5)` when you can do `the_filtered_list = [i for i in the_list if i > 5]` directly? – Patrick Artner Sep 27 '19 at 07:53
  • @PatrickArtner because it is faster and more memory-efficient. See my edit with speed and memory usage comparison – Cloudomation Sep 27 '19 at 07:55
  • 1
    it should be noted that "memory efficient" depends on what you want to do further on - e.g. I don't see the point in `list(generator)` if you can have a `list` in the first place. – FObersteiner Sep 27 '19 at 08:10
  • @Cloudomation `list(i for i in the_list if i > 5)` is slower than `[i for i in the_list if i > 5]` – splash58 Sep 27 '19 at 08:19
1

The problem is you are appending the list, not the number 'i'

the_list = [1, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7]
print(the_list)

k=5
tl2=[]
# the_list refers to the entire list
# i is an element in the list
for i in the_list:
   if i > k :
        # append  the number 'i'  if it is greater than k
        tl2.append(i)
print (t12)
Bhawan
  • 2,441
  • 3
  • 22
  • 47
0
the_list = [1, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7]
print(the_list)
k=5
new_ls = [x for x in the_list if x >k]
print(new_ls)

try this solution

Vashdev Heerani
  • 660
  • 7
  • 21