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 that are larger than what came before in a list with Python

2 Answers

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

tmp_list = [lst[i] for i in range(1, len(lst)) if lst[i] > lst[i - 1]]

lst = tmp_list

print(lst)



'''
run:

[89, 90, 51, 80, 90, 95]

'''

 



answered Jan 30, 2024 by avibootz
0 votes
lst = [23, 89, 90, 17, 51, 19, 80, 90, 70, 14, 95]
 
tmp_list = []
for index, value in enumerate(lst[1:], 1):
    print(value, ">" , lst[index - 1], " ", end="") 
    if value > lst[index - 1]:
        tmp_list.append(value)
        print("( append", value, ")") 

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

 



answered Jan 30, 2024 by avibootz
...