Monday, 16 September 2019

Lists in Python | Python List

Python List | List in Python




Python Lists are containers that are used to store a list of values of any type. 

Lists are mutable.It means you can change the elements of a list.   

Python will not create a separate list when you make changes to an element of a list.

A list is a datatype of Python that can store a sequence of values of any datatype.

Note Lists are similar to strings in many ways like indexing,slicing and accessing individual elements but they are different in the sense that Lists are mutable and Strings are immutable.

How to create list in python ?


To create a list, put a number of expressions in square brackets. We use of square brackets for creating list, It also indicate the start and end of the list and separate the elements by commas.   
  • Empty List
  • Mixed List
  • Nested List

python list



Empty List

The empty list is []. It is the list equivalent of 0 or ' ' and like them it also has truth value as false. You can also create an empty list as :

L=list()

or 

L=[]

It will generate an empty list and name that list as L.



Mixed List


The mixed list contains mixed type of elements. 
You can also create mixed list as :

L=[2,3,4,'P',3,4.5,6.7,'Python',3,2,9,2]

A Mixed list can have integer,float and string datatypes elements.



Nested List

A list can have an element in it, which itself is a list. Such a list is called nested list.

for example:

L=[3,4,[5,6],7,8]


L is a nested list with 5 elements: 3, 4, [5,6], 7, 8.



how to create nested list python

Creating Lists from Existing Sequences

You can also create list from existing sequences like string, tuple and lists.

L=list(<string|tuple|list>)

for example:

L=list("Hello")
print(L)

tup=(2,3,4,5)
L=list(tup)
print(L)

We can also create list using input method by user.

L=list(input("Enter List"))
print(L)


list created in python


Note if you create a list entered by a user using keyboard. The datatype of all the entered elements is String even though user entered digits.
If you want to enter a list of integers through keyboard, you can use the method given below:

L=eval(input("Enter list"))
print(L)

Please note sometimes eval() does not work in python shell. At that time you can run it on a script.

How to access list | Indexing Lists



Lists are sequences just like strings.They also index their individual elements just like strings do i.e., forward indexing as 0,1,2,3,..... and backward indexing as -1,-2,-3,...

how  to access nested list
  

how to index nested list python



Thus, you can access the list elements just like you access a python strings's elements.

for example:

L=[2,3,4,5,5]
print(L[])
print(L[:])

L[i] will give you the element at ith  index of the list .
L[a:b] will give you elements between indexes a to b-1 and so on...


indexing lists


As mentioned above image , the individual elements of a list are accessed through their indexes.
Note if you give index outside its range while accessing individual elements. Python will raise Index Error like strings.


Lets take nested list example how to access elements:

L=[2,3,4,[3,5],'hello',6.7,8.9,'p',['python']]
print(L[3])
print(L[4])
print(L[6])
print(L[8])

how to index list of strings


How to modify list ?





Slicing Lists

List slices, like string slices are the sub part of a list extracted out. You can use indexes of list elements to create list slices.

for example :

L=[1,2,3,4,5]
print(L[3:5])





No comments:

Post a Comment

Recent Post

Python Project | Banking System | Project on Banking System using python

  PYTHON PROJECT Banking System import csv import pandas as pd import matplotlib.pyplot as plt import sys import datetime found=False def ne...