Codied To Clipboard !
Home > Notes > python
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 [0], the second item has index [1] 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)
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])
here using The len() function of list we can easily loop dynamic wise.
list1 = [10,20,30,40,50] for i in range(0,len(list1)): print(list1[i])
The below code shows direct looping.
list1 = [10,20,22,56] for i in list1: print(i)
List len() function
list1 = [10,20,30,40] print(list1[0]) print(list1[1]) print(list1[2]) print(list1[3])
The count() is used to count the number of occurence of given value in list.
list1 = [10,20,30,40,50] num = 10 res1 = list1.count(num) print(res1)
To change the value of a specific item, refer to the index number:
thisList = ["apple","banana","cherry"] thisList [1] = "BlackCurrent" print(thisList)
To change the value of items within a specific range, define a list with the new values, and refer to the range of index numbers where you want to insert the new values:
thisList = ["apple","banana","cherry","orange","kiwi","mango"] thisList[1:3] = ["blackcurrent","watermelon"] print(thisList)
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 [0], the second item has index [1] 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)
the append() is used to add the items last in the list
list1 = [10,20,30] list1.append(40) print(list1)
To insert a list item at a specified index, use the insert() method. The insert() method inserts an item at the specified index:
thisList = [1,2,3] thisList.insert(1,7) print(thisList)
The 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 = [1,2,3,4,3,1] del list1[0] print(list1)
The sort() is used to sort the list in ascending and descending order
list1 = [1,5,4,7,9] list1.sort()
You cannot copy a list simply by typing l2 = l1, because: l2 here only be a reference to l1, and changes done in l1 will automatically also be made in l2. There are ways to make a copy, one way is to use the built-in List method copy().
l1 = [10,20,30,40] res1 = l1.copy() print(res1)
Using ‘+’ operator we can concate two lists.
list1 = [10,20,30] list2 = [55,44,66] res1 = list1 + list2 print(res1)
Use the 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()
list1 = [1,2,3,4,3] list1.reverse() print(list1)