top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Writer's pictureMariam Ahmed

Lists in Python


The list is a data structure in Python that is a mutable, ordered sequence of elements. It is one of 4 built-in data types in Python. It is used to store collections of data. Its items' index is zero-based.


The list denotes by [] brackets and is heterogeneous as follows:

List = ['Hana', 35, True]

The new list may define using list () i.e.:

Fruit = "Apple", "Banana", "Pear"
FruitList = list(Fruit)

It allows duplicate members as follows:

List = ['Good', 'Fail', 'Very Good', 'Good', 'Good', 'Fail']

The items in a list can be accessed or changed through indexing and slicing. Accessing and Changing by indexing as follows:

List = ['red', 'blue', 'green']
List [1] = 'black'

Then the list will become:

List = ['red', 'black', 'green']
List[-1] = 'white'

Then the list will become:

List = ['red', 'black', 'white']

Accessing by slicing may be as the following example:

List = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
SmallLst = List[:4]

Note it selects all elements from the start with index 0 to the element with index 3 and ignores the element with index 4.


So, it will contain [10, 20, 30, 40].

MedLst = List[4:7]

Note it selects all elements from the element with index 4 to the element with index 6 and ignores the element with index 7.

So, it will contain [50, 60, 70].

LargeLst = List[7:]

Note it selects all elements from the element with index 7 to the end of the list.


So, it will contain [80, 90, 100].


We can copy a list of items in another list using slicing as follows:

NewLst = List[:]

So, it will contain [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]


We can do the same thing as in the previous statement by writing in the end index of the period number greater than the length of the list and it won't get any error messages as follows:

NewLst2 = [:50]

So, it will contain also [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]


We can go throw the list with a specific step number that makes us jump from some elements and ignores these as follows:

QuickLst = List[::3]

So, it will contain [30, 60, 90]


There are some important functions that are used with the lists i.e.:

1. Adding elements to the list using the following functions:


1.1 append(): to add an element to the end of the list.

Lst = [1, 2, 3]
Lst.append(4)

So it will become: [1, 2, 3, 4]


1.2 insert(): to add an element to a specific place in the list.

The syntax is:

insert(index, object)

If we use the previous list as follows:

Lst.Insert(2, 5)

It will become:

[1, 2, 5, 3, 4]


1.3 extend(): to a list to the end of another list.

If we use the previous list as follows:

Lst.extend([6, 7, 8])

It will become:

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


2. Removing elements from a List using the following functions:


2.1 remove(item): to remove the first occurrence of the item from the list.

Lst.remove(1)

It will become:

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


2.2 clear(): it turns any list into an empty list.

Lst.clear()

It will become:

[]


2.3 del list_name: delete the entire list.

del Lst

Then the output of:

Print(Lst)

Will become:

NameError: name 'Lst' is not defined.  

3. Finding an element in the list using the function as follows:


index(): it returns the first occurrence of the element or returns ValueError if the element does not exist.

Lst = [1, 5, 7,5]
Lst.index(5)

It will return:

 1

That's it, I hope this article was worth reading and helped you acquire new knowledge no matter how small.


Feel free to check up on the notebook. You can find the results of code samples in this post.

0 comments

Recent Posts

See All

Comments


bottom of page