Machine Learning in Cloud

Machine Learning in Cloud

List

List is one of the compound data types in Python. Lists can group together elements.

A list is written as a comma-separated , elements (values or items) between square brackets[]. Lists might contain elements of different types, but usually the elements all have the same type.

# empty list
my_list = []

# list of integers
my_list = [1, 2, 3, 4]

# list of strings
my_list = [\'Krakow\', \'Warsaw\', \'Poznan\', \'Wroclaw\']

# list with mixed datatypes
my_list = [\'Krakow\', 2, 3.86, True]

Python lists are:

  • Lists can be empty
  • Lists can have any elements
  • Lists are ordered
  • List elements can be accessed by index
  • Lists can be nested to any arbitrary depth
  • Lists are mutable, i.e. they can be modified after they are created

Lists can be empty

An empty list can be created by assigning empty square brackets to an identifier.

# empty list
my_list = []

Lists can have any elements

Lists can contain a single datatype or a mixture of datatypes.

# list of integers
my_list = [1, 2, 3, 4]

# list of floats
my_list = [1.2, 2.4, 3.6, 4.8]

# list of strings
my_cities = [\'Krakow\', \'Warsaw\', \'Poznan\', \'Wroclaw\']

# list with mixed datatypes
my_list = [\'Krakow\', 2, 3.86, True]

Lists are ordered

The order of elements in a list is important. Two lists are not the same if the elements within a list are same but in a different order.

list_01 = [1, 2, 3, 4]
list_02 = [4, 3, 2, 1]

These two lists have the same elements but in different order

list_01 == list_02

Output:

False

You can use is keyword too.

list_01 is list_02

Output:

False

List elements can be accessed by index

You can use the index operator of square brackets[] to access an element in a list. In Python, index starts from 0.

Index number should be an integer, otherwise, it raises TypeError. Python will raise an IndexError if the index element is outside of the length of the list minus one.

my_cities = [\'Krakow\', \'Warsaw\', \'Poznan\', \'Wroclaw\']

Positive Indexing

The indices for the elements are as follow:

\"\"/
Positive Index

You can access each element by writing the name of the listfollowed by square brackets []and index numberof the element.

my_cities[0]
my_cities[3]

Outputs:

#my_cities[0]
\'Krakow\'

#my_cities[3]
\'Wroclaw\'

Negative indexing

Python allows negative indexing for elements in a list. Negative index counts from the end of the list. -1 is the index of last element, -2 is the index of second from last element, so on.

\"\"/
Negative Index

You can access elements in a list using negative index.

my_cities[-1]
my_cities[-3]

Outputs:

#my_cities[-1]
\'Wroclaw\'

#my_cities[-3]
\'Warsaw\'

List slicing in Python

Slicing of a list needs two indices: start index and end index separated by a colon :

All slice operations return a new list containing the requested elements. This means that the following slice returns a shallow copy of the list.

my_list = [2, 3, 4, 5, 5, 7, 8, 1,5, 6, 2, 9, 0]

You create a new list using slice operations. Square brackets [] with colon : will select all elements in the list and assign it to a new list

new_list = my_list[:]

Let\’s compare my_list with new_list

#== comparison
my_list == new_list

Output:

True
#is keyword comparison
my_list is new_list

Output:

False

While comparing new_list with my_list with ==, Python returns True but comparison with Python keyword is returns False. == compares the orders of elements in the my_list and new_list but Python is keyword is referencing memory locations of the my_list and new_list which are different and returns False.

Slicing to select/filter elements

Let\’s say you want to get the 2nd to 7th elements. Our start index is 1 and end index is 7 (End index returns the index before index 7, so in this case until index 6).

# value 2nd to 7th
start_index = 1
end_index = 7
my_list[start_index:end_index]
my_list[1:7] # you can write it this way too.

Output:

[3, 4, 5, 5, 7, 8]

Lists usually start counting from left to right. Empty start index location means select/filter elements from the start.

# value beginning to 6th
my_list[:6]

Output:

[2, 3, 4, 5, 5, 7]

Empty end index location means select/filter the elements from the start index to the last element in the list.

# value 6th to the end
my_list[5:]

Output:

[7, 8, 1, 5, 6, 2, 9, 0]

You can use a mixture of index and negative index together. For example; let\’s select the 2nd element from start until the 3rd element from the end. 2nd element has an index of 1 (Python starts at zero index) and 3rd element from the end has an index of -2, end index returns the (index – 1), meaning the index is bigger by one number.

# value 2nd from start until 2nd from the end
my_list[1:-2]

Output:

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

Stride

You can specify a stride—either positive or negative. Stride is added to the square bracket as a second colon :, the stride form will be[start_index : end_index : step]. Step control defines how many elements to skip before selecting the next element.

my_list = [2, 3, 4, 5, 5, 7, 8, 1,5, 6, 2, 9, 0]

my_list[1:7:2] # this will select elements 2nd to 7th every two steps

Output:

[3, 5, 7]

List reverse

Python lets you to easily reverse lists

my_list = [2, 3, 4, 5, 5, 7, 8, 1,5, 6, 2, 9, 0]
my_list[::-1]

Output:

[0, 9, 2, 6, 5, 1, 8, 7, 5, 5, 4, 3, 2]

Lists can be nested

A list can contain sublists, which in turn can contain sublists themselves.

\"\"/
A list of lists with their indices

Each element in list or its sublists can be accessed with element index.

my_list = [\'a\', [\'b\', \'c\', \'d\'], \'e\', [[\'f\', \'g\'], \'h\'], \'i\', \'j\']
my_list[3] # Returns all sublists in the index number 3

Output

[[\'f\', \'g\'], \'h\']

Each sublist adds another square bracket to access its elements. my_list[3][1] returns index 3 of the main list and an element at index 1 of the sublist

Simply append another square bracket. All operations of slicing can also be performed on the second square bracket, etc.

my_list = [\'a\', [\'b\', \'c\', \'d\'], \'e\', [[\'f\', \'g\'], \'h\'], \'i\', \'j\']
my_list[3][1] # It returns index 3 of the main list and elements at index 1 of the sublist

Output

\'h\'

Next: Lists are mutable

Previous Article
Next Article