0% found this document useful (0 votes)
3 views

Python Lists

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

Python Lists

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

LISTS IN PYTHON

What is a List?

⚫A List is a compound data type, you can


group values together.
⚫A list contains items separated by commas
and enclosed with in square brackets – [ ]
⚫The items belonging to the list can be
heterogeneous type.
⚫Index starts at ‘0’.
⚫A list in Python can grow and shrink in size
Creation of List

⚫Creation of the list


⚪ list_str = ["apple", "banana", "cherry"]
⚪ list_int = [1,2,3]
⚪ list_mixed = [“London”, 12 , 56.78]
⚪ List_emp=[ ]
Accessing elements of list
⚫Accessing the list:
⚪ List indices start at 0.
⚪ If an index has a positive value it counts from
the beginning
⚪ If the index has a negative value it counts from
the end.
⚫Example
⚪ color_list=["RED", "Blue", "Green", "Black"]
Item Red Blue Green Black
Index(from left to right) 0 1 2 3

Index(from right to left) -4 -3 -2 -1


Accessing elements of list:

markslist=[54,66,89,21]

#print all the elements of the list


print(markslist) • While accessing List
elements, if you pass a
#access first element of list negative index, python
print(markslist[0]) adds the length of the
list to the index to get
#access last element element’s forward
print(markslist[-1]) index.
markslist[-
1+4]=marklist[3]=21
#access all element one by one
for ele in markslist:
print(ele)
List Operations

Python Results Description


Expression
len([1, 2, 3]) 3 Length
[1, 2, 3] + [4, 5, [1, 2, 3, 4, 5, 6] Concatenation
6]
['Hi!'] * 4 ['Hi!', 'Hi!', 'Hi!', Repetition
'Hi!']
3 in [1, 2, 3] True Membership
for x in [1, 2, 3]: 123 Iteration
print(x)
List Methods
⚫ Inserting items
⚪ append(): Adds an element at the end of the list
⚪ insert(): Adds an element at the specified position
⚫ Removing items
⚪ remove(): removes the specified item
⚪ pop(): removes the specified index or the last item if
index is not specified
⚪ del: deletes the list completely:
⚫ Joining two lists
⚪ Using operator ‘+’
⚪ extend(): Add the elements of a list (or any iterable),
to the end of the current list
List Methods

⚫ clear(): Removes all the elements from the


list
⚫ count(): Returns the number of elements
with the specified value
⚫ index(): Returns the index of the first
element with the specified value
⚫ reverse(): Reverses the order of the list
⚫ sort(): Sorts the list
Slicing of a List
Mandatory
parameters
Optional
parameters

1. Allows
1. The index
you of
tothe
1. The index of the
list where
select slicing
nth item
list where slicing
end. the
within
starts.
2. range
Default value-
start to sto
2. Default index-0
len(list)-1
p.
Examples

colors= ['red', 'green', 'blue', 'white', 'yellow']


print(colors[0:2])
['red', 'green']
print(colors[1:2])
['green']
print(colors[1:-2])
['green', 'blue']
print(colors[:3])
['red', 'green', 'blue']
print(colors[:])
List Comprehension

⚫List comprehensions is a pythonic way of


expressing a ‘for-loop’ that appends to a list
in a single line of code.
⚫list comprehensions hail from functional
programming.
⚫less verbose, clearer and more readable.

Keshav Memorial Institute of Technology


List Comprehension

⚫Syntax
⚪ my_new_list = [ expression for item in list ]

Keshav Memorial Institute of Technology


Python List Comprehension (Conditionals)

⚫Conditionals can enhance Python list


comprehensions significantly.
⚫They server the following purposes:
⚪ To filter a list, and
⚪ To modify items in a list.

1. Finding Common Items in Two Lists Using


List Comprehensions

Keshav Memorial Institute of Technology


Thank you.

Keshav Memorial Institute of Technology

You might also like