-6

Very simple (and potentially stupid question) What is 'List Slicing' in python and can someone please give an example of list slicing.

Thanks !

2 Answers2

1

List slicing is creating a new list containing the requested elements.

 a = ['spam', 'eggs', 100, 1234]

This means that the following slice returns a shallow copy of the list a:

>>> a[:]
['spam', 'eggs', 100, 1234]
Jack_of_All_Trades
  • 10,942
  • 18
  • 58
  • 88
0

An example would be accessing the first three items of an array like so:

arr[:3]

There are many other examples on this page:

http://docs.python.org/2/tutorial/introduction.html

Tom Swifty
  • 2,864
  • 2
  • 16
  • 25
  • Thanks for the anwer! Just one more question... how would I use this if I did not want to include the 3rd letter for example arr[:3] gives the output ar – user2883864 Oct 16 '13 at 17:29
  • If I'm understanding correctly, you'd use arr[:2] for just the first two letters... – Tom Swifty Oct 16 '13 at 17:38