Make copies of lists in certain positions with Python

3

Hello, please, I would like to copy a certain list in python. The total size of the list is 150 and I want to remove the last 50 items from the list. Here I will represent a list 100 times smaller, that is to say with 15 elements and that I want to remove the last 5.

l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

How could I do this in a loop? or while?

Thank you!

    
asked by anonymous 17.07.2016 / 03:39

3 answers

2

In fact to copy the last 5 they to another list:

l2 = l[-5:] # l2 = [11, 12, 13, 14, 15]

To remove the last 5:

del l[-5:] # l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    
17.07.2016 / 19:54
1

In Python version 2.7 just do so

l2= l[:-5]
    
17.07.2016 / 03:55
0

I found a solution, but I do not know if it's correct:

l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

percent_l = 0.5

TAM = len(l)
taxa_c = int(percent_l * TAM)

print("taxa_c",taxa_c)

new_list = []

for i in l[0:taxa_c]:
    new_list.append(i)

print("new_list", new_list)

In this case I am calculating for a fee, the last percentage I would like to remove.

    
17.07.2016 / 03:55