List

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

LIST

 Lists are used to store multiple items in a single variable.


 Lists are one of 4 built-in data types in Python used to store collections of data.
 Lists are created using square brackets [].
 thislist = ["apple", "banana", "cherry"]
 print(thislist)
 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.

Ordered
 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.

Changeable
 The list is changeable, meaning that we can change, add, and remove items in a list after
it has been created.

Allow Duplicates
 Since lists are indexed, lists can have items with the same value:
 thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)

List Length
 To determine how many items a list has, use the len() function:
 hislist = ["apple", "banana", "cherry"]
print(len(thislist))

List Items - Data Types


 List items can be of any data type like String, int and boolean data types:
 list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]
 A list can contain different data types:
 list1 = ["abc", 34, True, 40, "male"]

type()
 lists are defined as objects with the data type 'list' : <class 'list'>
mylist = ["apple", "banana", "cherry"]
print(type(mylist))
<class 'list'>

The list() Constructor


 It is also possible to use the list() constructor when creating a new list.
thislist = list(("apple", "banana", "cherry"))
print(thislist)

['apple', 'banana', 'cherry']

Indexing and Slicing


The format for list slicing is of Python List Slicing is as follows:
Lst[ Initial : End : IndexJump ]
the above expression returns the portion of the list from index Initial to index End, at a
step size IndexJump.

Lst = [50, 70, 30, 20, 90, 10, 50]


print(Lst[::])
[50, 70, 30, 20, 90, 10, 50]

Lst = [50, 70, 30, 20, 90, 10, 50]


print(Lst[-7::1])
[50, 70, 30, 20, 90, 10, 50]

Lst = [50, 70, 30, 20, 90, 10, 50]


print(Lst[1:5])
[70, 30, 20, 90]

List = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print("Original List:\n", List)
print("\nSliced Lists: ")
print(List[3:9:2])
print(List[::2])
print(List[::])

Original List:
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Sliced Lists:
[4, 6, 8]
[1, 3, 5, 7, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

List Modification
L=[“one” , “two” ,”Three” ]
L[0:2]=[0,1]
L
[0,1,”Three”]

L[2]=3
L
[0,1,3]

Memory used by List

List takes more memory than string and time for processing.

FUNCTION OF LIST
1. len() Length of a List

l1=[1,2,3,4,5,6]
ll=len(l1)
print(ll)
6

2. list() A constructor with argument


ls=list('hello')
print(ls)
['h', 'e', 'l', 'l', 'o']

3. in, not in Membership operator return true or false


only
2 in l1
True
9 in l1
False
13 not in l1
True
4. +,* Concatenation and Replication Operator

List1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
List2 = [11, 12, 13, 14, 15, 16, 17, 18,
19]
print(List1 + List2)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14,
15, 16, 17, 18, 19]

List1 = [1, 2, 3, 4, 5, 6]
print(List1*3)
[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3,
4, 5, 6]

5. append() The append() method appends an element


to the end of the list

l1=[1,2,3,4,5,6,7]
l1.append(8)
l1
[1, 2, 3, 4, 5, 6, 7, 8]
6. del Its not a method, it’s a statement

del(l1[1:4])
l1
[1, 5, 6, 7, 8]
7. Index() The index() method returns the index of
the specified element in the list.
l1
[1, 5, 6, 7, 8]
l1.index(7)
3
8. Extend() The extend() method adds all the items of
the specified iterable, such as list, tuple,
dictionary, or string , to the end of a list.
t1=[100,200,300]
l1.extend(t1)
l1
[1, 5, 6, 7, 8, 100, 200, 300]
9. Insert(pos,<item>) The insert() method inserts an element to
the list at the specified index.
l1.insert(3,500)
l1
[1, 5, 6, 500, 7, 8, 100, 200, 300]
10. Pop() The list pop() method removes the item at
the specified index. The method also
returns the removed item.
l1
[1, 5, 6, 500, 7, 8, 100, 200, 300]
l1.pop()
300
l1
[1, 5, 6, 500, 7, 8, 100, 200]
l1.pop(3)
500
l1
[1, 5, 6, 7, 8, 100, 200]
11. Remove() The remove() method removes the first
matching element (which is passed as an
argument) from the list.
t2=[1,2,3,4,4,5,4,6,4,8,4]
t2.remove(4)
t2
[1, 2, 3, 4, 5, 4, 6, 4, 8, 4]
12. Clear() The clear() method removes all items
from the list.
t2.clear()
t2
[]
13. Count() The count() method returns the number of
times the specified element appears in the
list.
t2=[2,3,4,4,5,4,6,7]
t2.count(4)
3
14. Reverse() The reverse() method reverses the
elements of the list.
t2.reverse()
t2
[7, 6, 4, 5, 4, 4, 3, 2]
15. Sort() The list's sort() method sorts the elements
of a list always in ascending order, to get
descending order use reverse method.
t2
[7, 6, 4, 5, 4, 4, 3, 2]
t2.sort()
t2
[2, 3, 4, 4, 4, 5, 6, 7]
t2.sort(reverse=True)
t2
[7, 6, 5, 4, 4, 4, 3, 2]

You might also like