Python Lists I

Soumyajit Pal
Analytics Vidhya
Published in
4 min readJul 13, 2020

One of the standout features of Python is its powerful collection of built-in data structures, which can be classified as either mutable or immutable. Lists, Sets and Dictionaries are mutable, meaning their contents can be altered after creation. Tuples and Strings are immutable, i.e., initial values given to them during creation cannot be changed. In today’s post, let’s take a deep dive into world of Python Lists.

How do we create an empty list in Python?

Firstly, we need to associate a name with the list we wish to create. This name should be a legal identifier (only letters, underscore and digits are allowed; can start either with a letter or an underscore; cannot contain white-space; cannot be a keyword). Next, we use the list keyword followed by a pair of round brackets as shown below:

my_list = list()

Alternatively, we can use a pair of empty square brackets as shown below:

my_list = []

In both scenarios, we get an empty list identified by my_list

How do we create a list with values?

Mind you, lists are heterogeneous. They can contain different types of values or elements which are all of the same type as shown below:

my_list_1 = [1, 2, 3, 4]
my_list_2 = [3.14, 2.73, 1729, 28]
my_list_3 = [3.14, 2.73, "pi", 22, 7, "exponent"]

my_list_1 is a list of integers, my_list_2 is a list of floats and integers while my_list_3 is a list of integers, floats and strings

Can lists be indexed?

Indexing refers to accessing items of an ordered sequence by their positional values. Here, sequence can be any of the data structures available in Python such as lists and tuples. However, not all of them are ordered. A list is an ordered sequence, meaning that the order in which elements are created is preserved, thus making indexing possible. Python supports both positive and negative indexing:

my_list = [1.6, 2, "hello", 7]
print(my_list[0]) # OUTPUT : 1.6
print(my_list[3]) # OUTPUT : 7
print(my_list[-1]) # OUTPUT : 7
print(my_list[-2]) # OUTPUT : hello
print(my_list[2]) # OUTPUT : hello
print(my_list[10]) # OUTPUT : IndexError

Indexing starts from zero and goes up to one less than the length of the list. Trying to index a position for which there is no element in the list leads to an IndexError as shown above. Here, my_list contains 4 elements. Thus, the range of positional values is from 0 to (4 - 1) or 0 to 3. For reverse indexing, we can use negative values. As shown above, the last element 7 can be accessed using two positional values : 3 and -1.

print(my_list[len(my_list)-1])  # OUTPUT : 7
print(my_list[-1]) # OUTPUT : 7

The len( ) function reports the total number of elements present in a list. It returns 4 in the above scenario. Subtracting 1 from the length of the list gives us 3, which is the index of the last item in my_list. However, it turns out to be verbose. Negative indexing proves to be useful when we are dealing with a very large list and want to access the last item elegantly.

Can lists in Python be sliced?

Extracting a portion of an ordered sequence is known as slicing. Lists in Python are ordered and are candidates for the slicing operation. The syntax for slicing is :

name_of_list[start_index : stop_index : step_size]
start_index : starting point of slicing (inclusive)
stop_index : ending point of slicing (exclusive)
step_size : increment value

The slicing is inclusive of the starting position but it goes up to but not inclusive of the ending position. All three values are optional:

my_list = [1, 2, 3, 4, 5]  # list of 5 integersprint(my_list[0:3:1])  # OUTPUT: [1, 2, 3]print(my_list[:3:1])   # Default start_index = 0; OUTPUT: [1, 2, 3]print(my_list[0::1])   # Default stop_index = length of list
# OUTPUT: [1, 2, 3, 4, 5]
print(my_list[0:4:]) # Default step_size = 1; OUTPUT: [1, 2, 3, 4]print(my_list[::]) # Same as my_list[0:len(my_list):1]
# OUTPUT: [1, 2, 3, 4, 5]

The stop_index can be a position for which we have no element in the list. It will not lead to any IndexError. Instead, it will consider elements up to the end of the list:

print(my_list[0:10:1]) # OUTPUT: [1, 2, 3, 4, 5]

For slicing, we can use negative indexing too. An elegant way to reverse a list makes for a good example:

print(my_list[::-1])   # OUTPUT: [5, 4, 3, 2, 1]

I hope you found this post helpful. Please feel free to leave your comments, feedback, criticism, thoughts and everything else that comes along with them. See you soon!

--

--