Python Data Type: List

  • By
  • July 1, 2022
  • Python

Python Data Type: List

Python List:

Python lists are one of the most important data types and have many functions which can work with any datatype. Eg. [‘python’, ’java’, ‘cpp’]

Create python list:

In this section we will learn how to create a python list. In python a list is created by placing elements in square brackets [] separated by commas. My_list=[1,2,3].

List can contain any number of elements and they can be of different data types (integer, float, string,list,tuple,dict,etc). e.g. mylist=[1, ’name’, 10.4]

A list which contains another list as an item such a list is called a nested list. E.g [“mouse”, [7,8,21], 1000, ‘p’]

Access list element:

In the previous section we learned how to create a list, in this section we will learn how to access list elements.

There are various ways in which we can access the list elements.

For Free, Demo classes Call: 02071171500

Registration Link: Click Here!

List Index:

We can use index operator [] to access an item in a list. In python, indices start at 0. So, a list having 10 elements will have an index of 0 to 9.

If you try to access an index other than these will raise an index error. The indexes must be integers. We can’t use floats or other types. Which will give you type error. Nested lists are accessed using nested indexing.

My_list=[‘p’ ,’r’, ’o’, ’g’, ’r’, ’a’, ’m’, [10, 20, 30]]

Print(My_list[2])

This will print o

If I want to access 20 I will have to use my_list[7][1].

Print(my_list[8])

This will give you an error called type error. Because you are trying to access an index out of a list.

Print(my_list[3.0])

When you try to access an element like this you will get a type error saying indexes must be of type integer.

Negative Indexing:

Python allows negative indexing for its sequences (sequence data type). The index of -1 refers to the last item, -2 refers to the second last item.

e.g. my_list=[‘p’, ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’]

print(my_list[-1])

This will return element ‘m’ from the list.

Print(my_list[-8])

This will give me the element ‘p’ from the list.

List slicing in python:

We can access a range of items from the list by using the slicing operator ‘:’.

Print(my_list[2:5])

This will return me [‘o’, ‘g’, ‘r’]

This is exclusive of the last element.

Print(my_list[5:])

This will return elements from index 5 to the end.

Print(my_list[:])

Add/change list elements:

Lists are mutable, meaning their elements can be changed unlike string or tuple. We can use the assignment operator (=) to change an element or range of elements.

List1 = [1, 2, 3, 4]

List1[0] = 50

List1[1:3] = [30, 45, 15]

We can add one element in a list using the append method.

List1.append(55)

If we want to add several elements in a list we can use the extend method.

List1.extend([ 11, 22, 33, 44 ])

We can use the + operator to combine two lists. This is called concatenation. The * operator repeats a list for a given number of times.

Print(List1 + [21, 31, 41])

This will combine list1 elements with this other list.

Print( [ ‘py’ ] * 3 )

[ ‘py’, ‘py’, ‘py’ ]

This will copy elements that many times.

Till now we inserted data at the end, if we want to insert data at a particular location we can do that also using the insert function.

Or insert multiple items by squeezing it into an empty slice of a list.

List2 = [1, 3]

List2.insert(1,20)

Using this insert function, I will add 20 at index 1. So, now my list is [1, 20, 3].

For Free, Demo classes Call: 02071171500

Registration Link: Click Here!

Delete list Elements:

We can delete one or more items from a list using the python del statement. It can even delete the string entirely.

My_list = [‘p’, ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’]

del my_list[2]

this will delete ‘o’

del my_list[1:5]

This will delete multiple items from the list.

del my_list

This will delete my entire list.

We can use the remove function to remove the given element or the pop function to remove an element at a given index.

The pop function removes and returns the last item if the index is not provided. If we want to empty the whole list we can use the clear() function.

My_list = [‘p’, ‘r’, ‘o’, ‘b’, ‘l’, ‘e’, ‘m’]

My_list.remove(‘p’)

This will remove ‘p’ from the list.

My_list.pop(1)

This will return and remove ‘o’ from the list.

My_list.pop()

This will return and remove the last item from the list.

My_list.clear()

This will return an empty list. This function empties all the data from the list.

List comprehension: Elegant way to create new list from existing list

List comprehension is an elegant and concise way to create a new list from an existing list in python.

List comprehension consists of an expression followed by for statement in square brackets.

My_list = [1, 2, 3, 4, 5]

Using this list, I am creating a new list using list comprehension.

My_list2 = [ i*10 for i in my_list]

My_list2

This will return me this new list [ 10, 20, 30, 40, 50]

Using this simple one line method I could create new list, that’s why it is called elegent way to create new list.

Other list operations:

List membership test:

We can test if an item exists in a list or not using keywords in.

My_list = [ ‘p’, ‘r’, ‘o’, ‘b’, ‘l’, ‘e’, ‘m’ ]

Print(‘p’ in my_list)

This will return true as we have ‘p’ in my_list.

Print(‘x’ in my_list)

This will return false as we don’t have ‘x’ in my_list.

Print(‘x’ not in my_list)

This will return true as we don’t have ‘x’ in my_list and we are checking the same.

For Free, Demo classes Call: 02071171500

Registration Link: Click Here!

 

Iterating through list:

We can iterate through each item in a list using a for loop.

for i in my_list:

print(“character is = ”,i)

This will return each character from the list. As follows-

character is ‘p’

character is ‘r’

character is ‘o’

 

character is ‘b’

character is ‘l’

character is ‘e’

character is ‘m’

This is all about List data types. This is where we learned everything about lists. I hope all information is informative to you and you understood. In the next blog I will see the next datatype.

 

Author:-

Priyanka Tapadiya
Call the Trainer and Book your free demo Class 

Call now!!! | SevenMentor Pvt Ltd.

© Copyright 2021 | Sevenmentor Pvt Ltd.

 

 

Submit Comment

Your email address will not be published. Required fields are marked *

*
*