Working with a list using Python is fun.
Task for today: Return the first two list items, but don’t change the list.
Return first two items from the list
givenlist = [1,2,3,4,5,6,7,8,9] def returnList(givenlist): return givenlist[:2] print (givenlist) print (returnList(givenlist))
[:2] displays only first two elements but don’t change the list. As you can see, givenlist remains the same.
Return list without element
In case you want to remove an item from the list, use the remove method.
givenlist = [1, 2, 3, 4, 5, 6, 7, 8, 9] givenlist.remove(2) print(givenlist)