Python – Lists

Python Lists

We would cover Python Lists under the following:

  1. Introduction to Python List
  2. Creating a List
  3. Accessing List values
  4. Modifying a List
  5. Simple List Operations
  6. Indexing, Slicing and Matrices
  7. Python List Functions
  8. Python List Methods
  9. Watch the Video

 

1. Introduction to Python Lists

Lists are probably one of the most important data structures in Python you would learn. So pay close attention to the details.
A list is just a sequence of elements. Each element in the sequence has an index which starts from 0. So the first element is at index 0, the second is at index 1, the third element is at index 2 and so on.

Some of the operations you can perform on a list includes:

  • indexing
  • slicing
  • multiplying
  • checking for membership

 

 

2. Creating a List

A list in Python is created using square brackets [ ]. The elements are placed inside the square brackets and separated by commas.

For example:

 

names = ['Solace', 'Onyx', 'Trust', 'PraiseJah']
grades = ['A', 'B', 'C', 'D', 'D', 10, 20]
scores = [98, 57, 74, 90, 70]

 

Notice that in the grades list, characters are mixed with integers. So a Python list can contain combination of different data types.

 

 

3. Accessing List Values

You can access individual elements in a list. You do this using the indexes. The index helps you access the value of the element in that index.

For example

Using the lists we created above:

  • names[0] would give Solace
  • names[2] would give Trust
  • scores[4] would give 70

I recommend you try these yourself.

 

 

4. Modifying a List

You can actually change individual elements in a list. You do this using the assignment operator. You can also modify a list by adding new elements to the list. You add new elements using the append() method. Lets take some examples.

 

Updating existing elements

The code below updates the first two names in the list

# updates the existing names in the list
names[0] = "Solace Okeke"
names[1] = "Onyx Okeke"

 

Adding new elements

The code below add two more names to the list of names

# Add two more names to the names list
names.append("Jadon")
names.append(("Treasure"))

 

Deleting element

The code below deletes the specified elements from the list

# Remove some names from the list
names.remove("Solace")
names.remove(("Onyx"))
del names[2]  # removes the element at index 2

 

 

5. Simple List Operations

Just like in strings, the * and the + operators can be used with lists as well.  The + operator combines two elements or two lists into one. The * (repetition) operation repeats an element or list the given number of times.

The table below shows some of the basic operations you can perform on a list.

Python statement output description
len([1, 2, 3]) 3 Length
[2, 3, 4] + [5, 6, 7] [2, 3, 4, 5, 6, 7] Concatenation
[‘Hello’] * 2 [‘Hello’, ‘Hello’] Repetition
10 in [10, 20, 30] True Membership
for x in [10, 20, 30]: print x, 10 20 30 Iteration

 

 

6. Indexing, Slicing, and Matrices

You already know about indexing. That is accessing an element in a list using the index of the element. Now slicing is accessing or extracting a section of the list. Matrices also works similar to slicing.

For example, if we have the list below:

places = ["Enugu", "Owerri", "Abuja", "Lagos"]

 

Then the table below shows how you can perform indexing and slicing operations

Python statement Output Description
places[2] Abuja Offsets start from zero
places[-2] Abuja Negative: count from right
places[1:] [‘Owerri’, ‘Abuja’, ‘Lagos’] Slicing gets  a section

 

 

7. Python List Functions

Python provides various functions you can use. For example, getting the number of elements in the list. Or maybe finding the maximum element in a list of numbers.

The table below provides the the function available for Python list

SN. Function and brief description
1 cmp(list1, list2): No longer available in Python 3.
2 len(list): Returns the number of elements in the list.
3 max(list): Returns item from the list with maximum value.
4 min(list): Returns item from the list with minimum value.
5 list(seq): Converts a tuple to a list. Tuples are discussed in the next section.

 

 

8. Python List Methods

List methods further allow you to perform other operations on a list. The table below give a list of useful Python methods

SN Methods and brief description
1 list.append(obj): Appends the specified object to the list
2 list.count(obj): Returns count of how many times the specified object occurs in list
3 list.extend(seq): Appends the contents of seq to the  list
4 list.index(obj): Returns the lowest index in list that the specified obj appears
5 list.insert(index, obj): Inserts object obj into list at the specified offset index
6 list.pop(obj=list[-1]): Removes and returns last object or obj from the list
7 list.remove(obj): Removes object specified obj from the list
8 list.reverse(): Reverses the element of the list in place
9 list.sort([func]):  Sorts objects of list.  Uses compare func if provided

 

9. Watch the Video