List the Python WorkHorse: Part 1

mrcoderj

Ebeh Elisha

Posted on May 21, 2020

List the Python WorkHorse: Part 1

Alt Text
Source: Duolmy

Python has several built-in sequences of which list is one of them. Others include tuples and dictionaries.
One topic in python you need to understand before being able to code along with python is the python list.

LIST:

The list is one out of many python data structures, understanding the python list gives you upper hands in getting along with python programming language. Just like other C-Level languages, the python list can be seen as an array.
One unique thing about a List in python is that it can contain an element of many data type, i.e we can have a list of both an int, a string, and another list

In this tutorial we are going to cover the following.

  • The Python LIST
  • LIST Indexing
  • LIST Slicing
  • Operations with LIST
  • Checking for Membership in a LIST
  • Looping through a LIST

The Python List:

List is one of Python’s most powerful features readily accessible to new programmers, and they tie together many important concepts in programming. It is a collection of items in a particular order, the items can include an int, a boolean, a string and even another list.
A list can include numbers 0 - 9, names of things, people and also a boolean i.e True, False
In Python, Square bracket([ ]) indicates a list, each element/item of a list is separated by a comma with a unique index for identifying each element of the list.

Example of a list in python:

>>> myList = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
>>> print(myList)
output: [1, 2, 3, 4, 5, 6, 7, 8, 9]


>>> listOfCountries = [ China, USA, Greece, Madagascar, Columbia]
>>> print(listOfCountries)
output: [ China, USA, Greece, Madagascar, Columbia]


>>> listOfItems = [True, False, white, black, 12, 3, 5]
>>> print(listOfItems)
output: [True, False, white, black, 12, 3, 5]

A list can also contain another list in it:
See the example below:

>>> listOflist= [True, [False, white] , black, [1, 3, 5, 6]]
>>> print(listOflist)
output: [True, [False, white] , black, [1, 3, 5, 6]]

Each item in a list has an index which indicates its position in the list. You can access individual items in a List through their index, this leads us to what we know as List indexing.

List Indexing:

Alt Text
Source:LearnBATTA

List indexing is the process of assigning indexes to elements/items of a list, though this indexing is not done manually.
In python the list also possesses forwarding index and backward index, the backward indexing helps access the last element from the back i.e we can access the last item in the list without knowing the length of the list, .
We can access the index backward by making use of negative numbers, -1 indicates the last index of the list, this makes it easier to have a grasp of the last item in the list in case of a large list.

The index of a List starts with zero and proceeds to the last item of the list i.e the first element of the list has an index of 0.

>>> myList = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
>>> # To access the first element of  myList 
>>> myList [ 0 ]
1 

>>> # To access the second element of  myList 
>>> myList [ 1 ]
2

>>> # To access the fourth element of  myList 
>>> myList [ 13]
4

Some simple list hacks to guide you.

  • The last element of the list can be accessed with -1
>>> myList [ -1 ]
9
  • You can print the whole list by adding “:” in the square bracket
>>> myList [ : ]
[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
  • List has the ability of changing the element of a list by accessing the index of the list and assigning a new value to it..
>>> myList [ 0 ] = 12
>>> print(myList)
Output: [ 12, 2, 3, 4, 5, 6, 7, 8, 9 ]

You’ll notice that the first index of the list has been changed to the value 12.
In the example above where we have the list inside a list, the inner list can also be accessed using the same method we applied in accessing an element of the list, but it varies when we want to access the values of those elements inside the list of a list.

See examples for clarification.

>>> listOflist= [True, [False, white] , black, [1, 3, 5, 6]]
>>> # To access the fourth element of  listOflist which also 
is a list 
>>> listOflist [ 3 ]
Output: [ 1, 3, 5, 6 ]

>>> # To access the first element of the fourth element of  listOflist which also is a list

>>> listOflist [ 3 ][ 1 ]
>>> print(listOflist) 
output: 1

'listOflist' contains other lists inside of it, and to access it you have to use two square brackets [ ] [ ] the first for the first list and the second to access the element of the second list

>>> listOflist [ 3 ][ 3 ]
>>> print(listOflist) 
output: 6

Selecting a range of items from the list is possible with the List Slicing.

List Slicing

Alt Text
Source: PHPLIST

When you hear about slicing in a list what comes into your mind: hope you were not thinking about slicing a loaf of bread into some pieces.
Slicing in Python List simply means selecting a range of items from our list. In select we have the starting point and the endpoint i.e the start index and the end index.

Alt Text
Source: Learnbyexample

In the image above the start means the starting index of which you want to start selecting an element from the list, then the “stop”, it indicates the last index to be selected.

Note: In list slicing, the start index is inclusive whereas the stop index is exclusive, this means that we start from the start index to the index before the stop index as the stop index is not selected. For example.

>> mylist [ 0 : 3 ]
[ 12, 2, 3]

From the example above we see that the index 3 is not selected.

Some tips to help you in List Slicing

  • Selecting all the element of a list
>>>  myList [ : ]
Output: [ 12, 2, 3, 4, 5, 6, 7,
  • Selecting from the first element to a specified index
>>> myList [ : 4 ]  # or
>>> myList [ 0 : 4 ]
Output: [ 12, 2, 3, 4 ]
  • Selecting from a start index to the last index
>>> myList [ 2 : ]
Output: [  3, 4, 5, 6, 7, 8, 9 ]
  • Selecting the last item of a list:
>> myList [ - 1 ]
Output: 9

Slicing List with With Step:

In the picture of the list slicing above, we have the start, the stop and the step. In list slicing we can include a step in selecting the range of items in our list.
Let's look in the following examples:

myList = [ 12, 2, 3, 4, 5, 6, 7, 8, 9 ]
>> myList [ 0 :  : 2 ]
Output: [ 12,  3,  5,  7,  9 ]

What actually happened here!
Yeah the start index is 0, : represents the last index, whereas the 2 is the step, what actually is the step doing, it means that in selecting the range of items after selecting the start index the next index should be omitted and the next selected and so on.

Checking For Membership in a List

Checking if an element belongs to the list and if it does not belong to the list. This can be done with the “IN” and “NOT IN”. Let’s dive into the example for better understanding.
Example:
Using the “in” keyword

>> listOfItems = ["Dav", "Dan", "Joe", "Jane", 2, True, False ]
>> "Dave" in listOfItems
# Output: Fasle

>>"Dav" in listOfItems
# output: True

This is False because we can't find Dave in the list, instead we have Dav
Using the “not in” keyword

>> "Dav" not in listOfItems
# Output: False
# More example:
>> 3 not in listOfItems
# Output: False

Hope you understood this, if an item is in the list it returns True, whereas if the item is not in the list it returns a False and vice-versa.
Before we end this section let's talk about printing out individual items of the LIST using a for loop.

Looping Through a List

Although this could have come after learning how to loop through an item, nevertheless let’s talk about iterating over a list to print each item of the list.
Looping through a list can be achieved with the help of the FOR LOOP.
Coming from a C-Like language like Java, the for loop looks somehow complex, but in python the for loop though the same concept is easier.

Example of a for loop in python:

>>> for i in range(5):
...       print(i)
Output: 0 1 2 3 4 
>>> myList = [ 2, 3, 5, 4, 6, 7, 8 ]
>>> for i in myList:
...    print ( i )
Output: 2 3 5 4 6 7 8.
listOfNames = [ 'dan', 'james',  'joan', 'sean', 'cathy', julie' ]
>>>for i in listOfNames:
..... print ( i )
Output: Dan James Joan Sean Cathy Julie

The item of the list can be looped out to perform some operations with, such as looping through a list to print only even numbers, to print out all odd numbers etc. We are going to cover this in the next part of the tutorial and stay tuned.

Look out for the next Part: we will talk about list methods.
Thanks for reading through.

💖 💪 🙅 🚩
mrcoderj
Ebeh Elisha

Posted on May 21, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related