Lists are used to store multiple items in a single variable.
List items are ordered, changeable, and allow duplicate values.
List items are indexed, the first item has index , the second item has index  etc.
When we say that lists are ordered, it means that the items have a defined order, and that order will not change.
If you add new items to a list, the new items will be placed at the end of the list.
The list is changeable, meaning that we can change, add, and remove items in a list after it has been created.
Since lists are indexed, lists can have items with the same value means duplicates are allowed
list1 = [1,2,3,4,3,1] print(list1
Python List looping indexing Static way
In this Example we will pass the hard coded the starting and ending limit , which is treated as Static Looping , which is not advisable.
list1 = [1,2,3,4,5] for i in range(0,5): print(list1[i])
Python List looping indexing Dynamic way
here using The len() function of list we can easily loop dynamic wise.
list1 = [10,20,33,44,55] for i in range(0,len(list1)): print(list[i])
Looping the list without len
The below code shows direct looping.
list1 = [10,22,55,66] for i in list1: print(i)
Accessing the items in list
The below code shows the indexing of list
List len() function
list1 = [10,20,30,40] print(list1) print(list1) print(list1) print(list1)
len function is used to get the lengeth of the given list
list1 = [10,20,30,40] print(len(list1))
The count() is used to count the number of occurence of given value in list.
list1 = [10,20,30,40,10] num = 10 res1 = list1.count(num) print(res1)
Change Item Value
thislist = ["apple", "banana", "cherry"] thislist = "blackcurrant" print(thislist)
Change a Range of Item Values
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"] thislist[1:3] = ["blackcurrant", "watermelon"] print(thislist)
list1 = [10,20,30] list1.append(40) print(list1)
thislist = [1,2,3] thislist.insert(1, 7) print(thislist)
remove() method removes the specified item.
if duplicate items is there it will only delete the first occurence.
thislist = [7,9,10] thislist.remove(10) print(thislist)
del() with take the parameter as index and delete the value from list for specified position.
list1 = [10,20,30,50] del list1 print(list1)
List Without Comprehension
List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.
Based on a list of Game, you want a new list, containing only the Game with the character “c” in the Game list.
Without list comprehension you will have to write a
for statement with a conditional test inside:
Game_list = ["cricket","football","carme","Ludo"] newlist =  for x in Game_list: if "c" in x: newlist.append(x) print(newlist)
list1 = [1,5,4,7,5,9] list1.sort() print(list1)
l1 = [10,20,30,40] res = l1.copy() print(res)
Join Two Lists using '+' operators
Using ‘+’ operator we can concate two lists.
list1 = [10,20,30] list2 = [55,44,66] res = list1 + list2 print(res)
Join Two Lists using extends Function
extend() method to add list2 at the end of list1:
list1 = [7,8,9] list2 = [1, 2, 3] list1.extend(list2) print(list1)
List reverse() Method
reverse() , is used to reverse the list
list1 = [1,2,3,4,3] list1.reverse() print(list1)
Leave a Comment