Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

LISTS in Python

Uploaded by

Samridhii
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

LISTS in Python

Uploaded by

Samridhii
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

 List is a sequence of values called items or elements.

 The elements can be of any data type.


 The list is a most versatile data type available in
Python which can be written as a list of comma-
separated values (items) between square brackets.
 List are mutable, meaning, their elements can be
changed.
• In Python programming, a list is created by placing all the items
(elements) inside a square bracket [ ], separated by commas.
• It can have any number of items and they may be of different types
(integer, float, string etc.).

Method -1 without constructor Method-2 using list constructor


# empty list # empty list
my_list = [] my_list = list()
# list of integers # list of integers
my_list = [1, 2, 3] my_list = list([1, 2, 3])
# list with mixed datatypes
my_list = [1, "Hello", 3.4]
# nested list
my_list = [“welcome", [8, 4, 6]]
 Index operator [] is used to access an item in a list. Index starts from
0
 marks=[90,80,50,70,60]
 print(marks[0])
Output: 90
 Nested list:
 my_list = [“welcome", [8, 4, 6]]
 Print(marks[1][0])

Output: 8
 Python allows negative indexing for its sequences. The index of -

1 refers to the last item, -2 to the second last item and so on

my_list = ['p','r','o','b','e']

# Output: e
print(my_list[-1])

# Output: p
print(my_list[-5])
#Add Elements
 add one item to a list using append()
#Change Elements method
 add several items using extend()
= operator with index
 insert one item at a desired location by
>>> marks=[90,60,80] using the method insert()
>>> marks.append(50)
>>> print(marks) >>> print(marks)
[90, 60, 80] [90, 100, 80, 50]
>>> marks[1]=100
>>> marks.extend([60,80,70])
>>> print(marks)
>>> print(marks)
[90, 100, 80] [90, 100, 80, 50, 60, 80, 70]

>>> marks.insert(3,40)
>>> print(marks)
[90, 100, 80, 40, 50, 60, 80, 70]
 remove() method to remove the given item
 delete one or more items from a
list using the keyword del. >>> marks=[90,60,80]
>>> marks.remove(80)
 It can even delete the list entirely.
>>> print(marks)
>>> print(marks) [90, 60]
[90, 100, 80, 40, 50, 60, 80, 70] >>> marks.remove(100) delete items in a list
Traceback (most recent call last):
by assigning an
File "<pyshell#1>", line 1, in <module>
empty list to a slice
>>> del marks[6] elements.
marks.remove(100)
>>> print(marks) marks=[100,20,30]
ValueError: list.remove(x): x not in list >>> marks[1:2]=[]
[90, 100, 80, 40, 50, 60, 70]  pop() method to remove an item at the given index. >>> print(marks)
>>> del marks >>> marks=[100,20,30] [100, 30]
>>> marks.pop()
>>> print(marks)
30
Name Error: name 'marks' is not >>> print(marks)
defined
[100, 20]
 clear() method to empty a list.
>>> >>> marks.pop(0)
>>> marks.clear() 100
>>> print(marks) >>> print(marks)

[] [20]
 >>> marks.extend([40,50,60,70])
 >>> marks

 [90, 40, 50, 60, 70]


 >>> marks.pop()
 70

 >>> marks.pop()
 60
 Slicing [::] (i.e) list[start:stop:step]
 Concatenation = +

 Repetition= *
 Membership = in
 Identity = is
append() - Add an element to the end of the list

extend() - Add all elements of a list to the another list

insert() - Insert an item at the defined index

remove() - Removes an item from the list

pop() - Removes and returns an element at the given index

clear() - Removes all items from the list

index() - Returns the index of the first matched item

count() - Returns the count of number of items passed as an argument

sort() - Sort items in a list in ascending order

reverse() - Reverse the order of items in the list

copy() - Returns a shallow copy of the list


Function Description
Return True if all elements of the list are true (or if the list is
all()
empty).
Return True if any element of the list is true. If the list is empty,
any()
return False.
Return an enumerate object. It contains the index and value of all
enumerate()
the items of list as a tuple.
len() Return the length (the number of items) in the list.
list() Convert an iterable (tuple, string, set, dictionary) to a list.
max() Return the largest item in the list.
min() Return the smallest item in the list
sorted() Return a new sorted list (does not sort the list itself).
sum() Return the sum of all elements in the list.
copy()
reversed()

You might also like