Wednesday, August 1, 2018

(Python) Lists

In this article, we will discuss the concept of "Lists", which is one of the most useful concepts within the Python programming language.

Lists are very similar to vectors within the R programming language. In a prior article, I defined lists as:

A list is a collection of elements. Lists are ordered and modifiable.

For this to possess any meaning at all, we must delve into some example code.

Create a List

# Create List #

a = [ 'apple', 'orange', 'pear', 'kiwi', 'mango']


Lists, within Python, can contain both numeric and string variables.

# Create List with Mixed Variables #

b = [ 'apple', 1, 'pear', 2, 'mango']

# Print Both Lists #

print(a)

print(b)


It also possible to create a list which consists of multiple lists.

# Create Lists #

a = [ 'apple', 'orange', 'pear', 'kiwi', 'mango']

b = [ 'apple', 1, 'pear', 2, 'mango']

c = [a, b]

# Print New List #

print(c)


Console Output:

[['apple', 'orange', 'pear', 'kiwi', 'mango'], ['apple', 1, 'pear', 2, 'mango']]

Adding Elements to a List

Additional list elements can be added to the end of a list through the utilization of the append() function.

# Create List #

a = [ "apple", "orange", "pear", "kiwi", "mango"]

# Add element: "banana" to list: "a" #

a.append("banana")

# Print list: "a" #

print(a)


Console Output:

['apple', 'orange', 'pear', 'kiwi', 'mango', 'banana']

Referencing Select Elements from a List

To select an element from a list, you must first identify the position of the element. Against what is probably common sense, list elements begin with a reference position of “0”. To illustrate this concept, please view the explanation below:

a = [ "apple", "orange", "pear", "kiwi", "mango"]

“apple” is referenced by element: “0”.

“orange” is referenced by element: “1”.

“pear” is referenced by element: “2”.

“kiwi” is referenced by element: “3”.

“mango” is referenced by element: “4”.

Now, we will demonstrate the way in which to reference an element within a list.

# Print out an initial element from a list #

print(a[0])


Console Output:

apple

# Print out the last element from a list #

print(a[-1])


Console Output:

mango

If you wish to count the list elements in a backwards manner, you have the option of doing so through the utilization of negative values. To illustrate this concept, please view the explanation below:

“apple” is referenced by element: “-5”.

“orange” is referenced by element: “-4”.

“pear” is referenced by element: “-3”.

“kiwi” is referenced by element: “-2”.

“mango” is referenced by element: “-1”.

# Print out the first element from a list #

print(a[-5])


Console Output:

Apple

Referencing a Series of Elements from a List

There may be instances in which you would like to reference a series of elements contained within a variable list.

# Create List #

a = [ "apple", "orange", "pear", "kiwi", "mango"]

firsttwoelements = a[0:2]

print(firsttwoelements)


Console Output:

['apple', 'orange']

What the code “a[0:2]” is referring to, is a reference to elements “0” (apple) through “2” (pear), but not including element “2”.

lasttwoelements = a[-2:]

print(lasttwoelements)


Console Output:

['kiwi', 'mango']

What the code “a[-2:]” is referring to, is a reference to elements through “-2” (kiwi), including element -2.

Modifying List Elements

There may be instances in which a list element requires modification, this can be achieved through the utilization of the code below:

# Create List #

a = [ "apple", "orange", "pear", "kiwi", "mango"]

# Modify the initial list element #

a[0] = "dragon fruit"

# Modify the final list element #

a[-1] = "cherry"

# Print the modified list #

print(a)


Console Output:

['dragon fruit', 'orange', 'pear', 'kiwi', 'cherry']

Combining Lists

Let’s say that we wish to combine two lists into a single list which consists of elements of both prior components.

# Create Lists #

a = [ "apple", "orange", "pear", "kiwi", "mango"]

b = [ "apple", 1, "pear", 2, "mango"]

# Combine Lists #

c = a + b

# Print the modified list #

print(c)


Console Output:

['apple', 'orange', 'pear', 'kiwi', 'mango', 'apple', 1, 'pear', 2, 'mango']

Deleting Elements from a List

What if you wish to delete an element from a list? The following functions will allow you to achieve such.

# Create List #

a = [ "apple", "orange", "pear", "kiwi", "mango"]

# Remove the initial list element #

del(a[0])

# Print the modified list #

print(a)

# Remove the last element from the modified list #

a.pop()

# Print the modified list #

print(a)


This produces the output:

['orange', 'pear', 'kiwi']

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.