Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,961 questions

51,903 answers

573 users

How to leave only values in a list that are larger than what came before in all list with Python

2 Answers

0 votes
lst = [23, 89, 90, 17, 51, 19, 80, 90, 70, 14, 95]

tmp_list = [lst[0]]
for value in lst:
    print(value, ">" , tmp_list[-1], " ", end="") 
    if value > tmp_list[-1]:
        tmp_list.append(value)
        print("( append", value, ")") 

lst = tmp_list

print(lst)




'''
run:

23 > 23  89 > 23  ( append 89 )
90 > 89  ( append 90 )
17 > 90  51 > 90  19 > 90  80 > 90  90 > 90  70 > 90  14 > 90  95 > 90  ( append 95 )
[23, 89, 90, 95]

'''

 



answered Jan 30, 2024 by avibootz
0 votes
lst = [23, 89, 90, 17, 51, 19, 80, 90, 70, 14, 95]

tmp_list = [lst[0]]
for value in lst:
    print(value, ">" , tmp_list[-1], " ", end="") 
    if value > tmp_list[-1]:
        tmp_list.append(value)
        print("( append", value, ")") 

if (tmp_list[0] < lst[1]):
    tmp_list.remove(tmp_list[0])

lst = tmp_list

print(lst)
 
 
 
 
'''
run:
 
23 > 23  89 > 23  ( append 89 )
90 > 89  ( append 90 )
17 > 90  51 > 90  19 > 90  80 > 90  90 > 90  70 > 90  14 > 90  95 > 90  ( append 95 )
[89, 90, 95]
 
'''
 

 



answered Jan 30, 2024 by avibootz
...