Python,删除索引列表中的所有元素> ñ

时间:2017-02-10 18:08:10

标签: python indexing

说我有一个清单:

myList = [1,2,3,4,5,6,7,8]

现在,假设我有一个用户输入他们想要关闭列表的索引。如何删除索引大于输入值的列表中的所有元素,或者重新创建具有新边界的列表?

e.g)

x = input('Please enter index of last value in new list')
# say inputted value for x is 3
# I want to end the list at index 3 (i.e the new list will be [1,2,3,4])

获得目标的最快方法是什么?

1 个答案:

答案 0 :(得分:-2)

myList = [1,2,3,4,5,6,7,8]
x = input('Please enter index of last value in new list')
myList = myList[0:x+1]
print myList