list = [1, 6, 5, 7, 8]
num = int(input("Enter a value") #lets say 3 for now
How do I get the values from the start of the list to the num
?
Output should be:
[1, 6, 5]
list = [1, 6, 5, 7, 8]
num = int(input("Enter a value") #lets say 3 for now
How do I get the values from the start of the list to the num
?
Output should be:
[1, 6, 5]
You can use slicing. Slicing allows you to take a certain range of numbers from a list.
list = [1, 6, 5, 7, 8]
num = int(input("Enter a value")
print(list[0:num])
That should return : [1,6,5]
Hope that helps!
You can achieve this in a single line of code,
data = [1, 6, 5, 7, 8]
print(data[:int(input('Enter the number'))])
list[:3]
You can use slicing
to achieve this. Note: Don't name your list list
! Please name it something else, as you will not be able to call list()
anymore.