List the Python WorkHorse: Part 1
Tags: python
Published: 2020-05-21 16:29:28

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/403ar89ipbrh2zl25bty.jpeg)
Source: [Duolmy](https://www.blog.duomly.com/wp-content/uploads/2019/08/python-list-definition-and-usage.png)

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:


```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:

```python 
>>> 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](https://dev-to-uploads.s3.amazonaws.com/i/8o5xbc5pe0l4wz8cfv07.png)
Source:[LearnBATTA](https://learnbatta.com/assets/images/python/working_with_list_python.png)

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.
```python
>>> 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
```python
>>> myList [ -1 ]
9
```

+ You can print the whole list by adding “:” in the square bracket

```python
>>> 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..

```python
>>> 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.
```python
>>> 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
```python
>>> 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](https://dev-to-uploads.s3.amazonaws.com/i/lhjfd4l7a0o1mvj6kgnh.jpg)
Source: [PHPLIST](![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lhjfd4l7a0o1mvj6kgnh.jpg))

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](https://dev-to-uploads.s3.amazonaws.com/i/3hxzwq1oqik6nzvceo7l.png)
Source: [Learnbyexample](https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.learnbyexample.org%2Fpython-list-slicing%2F&psig=AOvVaw0zAKlQTVbsZnEbet0Xkffl&ust=1590159430075000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCPiWlN2bxekCFQAAAAAdAAAAABAG)

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.

```python
>> 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
```python
>>>  myList [ : ]
Output: [ 12, 2, 3, 4, 5, 6, 7,
```
+ Selecting from the first element to a specified index
```python
>>> myList [ : 4 ]  # or
>>> myList [ 0 : 4 ]
Output: [ 12, 2, 3, 4 ]
```
+ Selecting from a start index to the last index
```python
>>> myList [ 2 : ]
Output: [  3, 4, 5, 6, 7, 8, 9 ]
```

+ Selecting the last item of a list:
```python 
>> 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:

```python 
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**
```python
>> 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**
```python
>> "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:
```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.