Welcome To
Lists


Lists In Python

THe List In a python it store a collection of value of multiple type. and the list contains the collection of value stored in the single variable. then we access that variable in the python code . The list contains the heterogenous typed values. The values is called list elements and items in the python. list is allows a you to store the collection of value of any type it make python pawer full tool. some list Importance in the below

Importance Of Python List

As we saw in string the list is similar to string but some rules and python list syntax is different

The List Is a ordered and mutable collection of data .

The list Contain the collection of elements in different types.

We accessing that list value by using one technique is called Indexing

The Python list is allows a you to access the individual elements by passing the index value

The list contain the collection of data items that is enclose within the square brackets[] and each element is separated with commas

Creating Python List

We want to create a python list . by placing the collection elements in the square brackets and separated with comma , The Python provide the list to store the multiple typed data in the single variable. the list is a ordered and mutable which means we can change the list elements after that creating by using index value

Syntax

Variable_name=[ value1,value2,... value-n]
# some print code here

Example

my_List=[10,10.2,"hello"]
print(my_List)

Output

10
10.2
hello

Accessing List Elements

After creating the list we must be access that list in the python code. when we want to access the items in the list we must be know the indexing . using indexing to access the elements from the list . the list first element index is 0 and the list last element index is n-1 . n is number of elements in the list. passing the index value in this way . variable_name that is list name and slice operator that is [:] . inside the slice operator place index. Example in the below

Example

List=["hello", 10, 20,30,40.2]
print(List[0:2])

Output

[hello ,10 ,20 ]

Negative Indexing

The is Another way to access the elements from the list . negative indexing means it start from last element of list is represented by -1 and the the first element is represented by -n means the number of elements in the list . when we are using this negative indexing in the since we want to access the list elements from right to left. the Example in the below

Example

list=[10,20,30,"hello","Darshan"]
print(list[-3:-1])

Output

[30, 'hello']

Difference Between String And List

String List
The String is a sequence of character The List is a Sequence of different data items or Elements
The String is a Immutable which means once the string is created we cannot bew change The List is a mutable which means we can change the list elements after creating a list
The String is accessing using index value or Indexing The List elements also accessing using the indexing

List Are Mutable

The python list is a mutable which means we can change that list element after creating a list. when we want to change the list element we must be used the index. as below example

list=[10,20,30,"hello","Darshan"]
list[4]="Nandini"
print(list)

Output

[10, 20, 30, 'hello', 'Nandini']

Operations In Python List

  1. Traversing
  2. Concatenating
  3. Membership
  4. Comparison
  5. Slicing

Traversing In Python

The List is a iterable which means we using the for loop to print the each element in the list as sequentially . traversing means we print the list elements in one by one . Example in the below

Example

list=[10,20,30,"hello","Darshan"]
for i in list:
      print(i)

Output

10
20
30
hello
Darshan

Concatenation of list

Concatenating is a process of of adding the two list . and adding the new elements to the list . by using + operator to adding the two list to gather. syntax in the below

Syntax

list=list1+list2+........+list n

Example

list1=[10,20,30,"hello"] list2=[40,50,60,"darshan"]
list=list1+list2
print(list)

Output

[10, 20, 30, 'hello', 40, 50, 60, 'darshan']

Adding the new element to list

The adding the elements by using append() or insert(index) function

append()

The adding the new element to list by using append() it does not assigning index

Example

list1=[10,20,30,"hello"]
list1.append("Darshan")
print(list1)

Output

[10, 20, 30, 'hello', 'Darshan']

Insert()

Adding the new list element by specifying the index to store the new list element as show in the below example

Example

list1=[10,20,30,"hello"]
list1.insert(3,"darshan")
print(list1)

Output

[10, 20, 30, 'darshan', 'hello']

Removing list or list element

The removing the existing list or list element by using remove(value) function. as shown in the below example

Example

list1=[10,20,30,"hello"]
list1.remove(20)
print(list1)

Output

[10, 30, 'hello']

or

list1=[10,20,30,"hello"]
del list1
print(list1)

Repetition Of Lists

repeating the list by using the * (asterisk) operator . we can also assign the repetition value . as shown in the below example

Example

list1=[10,20,30,"hello"]
print(list1*3)

Output

[10, 20, 30, 'hello', 10, 20, 30, 'hello', 10, 20, 30, 'hello']

Membership Operator In Lists

The membership is used to we check the membership of an element from a list using membership operator That is membership in and membership not in . the membership operator checks the particular list element is belongs to the list or not

Membership in

It Checks the particular element is belongs to list or not . if that particular element is belongs to list then the output is true other wise false

Example

list1=[10,20,30,"hello"]
result=10 in list1
print(result)


output

True

Membership Not In

It is offsite to membership in

Example

list1=[10,20,30,"hello"]
result=10 not in list1
print(result)


Output

False

Comparison of a list

The comparison is a process of compare the two lists and return the value . you want to learn the comparison operators please go through the operator lesson . the comparison value should be boolean value either True OR False

Example In the Below

EXample

list1=[10,20,30]
list2=[20,40,30]
print(list1< list2)
print(list1>list2)
print(list1==list2)
print(list1<=list2)
print(list1>=list2)
print(list1!=list2)

output

True
False
False
True
False
True

Coping The List

THe Coping the list is a process of assigning the list variable element to another variable as shown in the below example

Example

list1=[10,20,30]
list2=list1
print(list2)

Output

[10, 20, 30]