0% found this document useful (0 votes)
16 views29 pages

Lists

Python lists allow storing collections of ordered and mutable data. They can contain duplicate elements and are accessed using indexes. Lists are created using square brackets and can contain elements of different data types. Common list operations include accessing elements, slicing lists, merging, appending, inserting, removing, sorting, finding length and sum, and copying lists. The in and not in operators check for an element's existence in a list.

Uploaded by

Abhimanyu Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views29 pages

Lists

Python lists allow storing collections of ordered and mutable data. They can contain duplicate elements and are accessed using indexes. Lists are created using square brackets and can contain elements of different data types. Common list operations include accessing elements, slicing lists, merging, appending, inserting, removing, sorting, finding length and sum, and copying lists. The in and not in operators check for an element's existence in a list.

Uploaded by

Abhimanyu Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Python Lists

Divya Bahirwani
• There are 4 built-in data types in
Python.
• They used to store collections of data
• All have different qualities and usage.
1.List
2.Tuple
3.Set
4.Dictionary
Lists
 List items are ordered,
 List items are mutable (elements can be changed in place)
 List items allow duplicate values.
Index
Indexing in Python is a way to refer the individual items within an
iterable (an object in which you can traverse through all the different
elements or entities contained within the object) by its position. In
other words, you can directly access your elements of choice within
an iterable and do various operations depending on your needs.

Orange Apple Banana Kiwi


Index 0 1 2 3
Creating a list
• Syntax
<listname> = [“value1” , “value2” , “value3”]
• Example
>>> fruitlist = [“orange” , “apple” , “banana” , “Kiwi”]
>>> print (fruitlist)
['orange', 'apple', 'banana', 'Kiwi']
• Numbers in a list are typed without quotes
• Letters/alphabets are typed in quotes
• Strings are typed in single/double quotes
• A list can have multiple data types
Examples

>>> a = []
>>> a = [1,2,3]
>>> a = [‘a’, ‘b’, ‘c’]
>>> a = [1, 2.5, 40 , 18.9]
>>> mixlist = [“Orange” , 12 , “Q” , “fruits” , “A-10”]
Creating a list from existing sequence
Syntax:
L = list(<sequence>)
• L is the list name
• List is function name
• Sequence is existing sequence of string
• Example:
>>> str = “hello”
>>> list1 = list(str)
>>> print (list1)
['h', 'e', 'l', 'l', 'o']
Accessing elements in a list
Syntax
<list name> [<index>]

Example: mixlist [2]


• Negative index can also be used to access
the elements
• Negative indices are counted from the
back of the list
• It starts with -1 and not 0
Orange Apple Banana Kiwi
Index -4 -3 -2 -1
0 1 2 3
Extracting a range from the list

Use slicing operator :


Syntax:
<listname> [index of starting element : last index to be accessed + 1]
Example – Extracting first 3 elements
Mixlist[:3]
 Nothing before the colon, is interpreted as 0
Also last position is excluded so add 1 to number of elements to be
accessed. In the example element at 3rd position will be excluded.
This will access elements having index from 0 to 2.
Extracting a range from the list

 If elements from a particular element till the end is to be extracted:


<listname>[index number:<space>]
Example: mixlist [2: ]
 This will extract all elements from index number 2 – till the last
element
 Here index 2 is included
What do we write is elements from index 2 to 4 have to be accessed?
Extracting a range from the list using negative index
There are 7 elements in the list
To access 4th and 5th value
print (mixlist [-4 : -2])
Elements at position -4 and -3 will be displayed
Element A B C D E F G
Index -7 -6 -5 -4 -3 -2 -1
Position 1 2 3 4 5 6 7
Extracting every second item in list
• Use colon twice and write 2
print (mixlist [: : 2]
Printing list in reverse order

• Use colon twice followed by -1


print (mixlist [: : -1]
Operations on Lists
1. Merging 2 lists
• Syntax
<new list name = <list1> + <list2>
• Example
l1 = [“a” , “b” , “c” , “d”]
l2 = [“aa” , “bb” , “cc” , “dd”]
newl = l1 + l2
print (newl)
Output:
[‘a’ , ‘b’ , ‘c’ , ‘d’ , ‘aa’ , ‘bb’ , ‘cc’ , ‘dd’]
2. Appending an item/element in list
• append method is used to add 1 element
• The new element is always added at the end.
• Syntax
<list name>.append (<element to be added>)
• Example: The previous list was-
mixlist = [“Orange” , 12 , “Q” , “fruits” , “A-10”]
Add a new element - colourful
Mixlist.append(“colourful”)
Print (mixlist)
[‘Orange’ , ’12’ , ‘Q’ , ‘fruits’ , ‘A-10’ , ’colourful]
3. Extending 1 list to another
• extend method is used
• Syntax
<list name>.extend = <list name whose elements have to extended>
• Example:
>>> list1 = ['a','b','c’]
>>> list2 = ['x','y','z’]
>>> list1.extend(list2)
>>> print(list1)
['a', 'b', 'c', 'x', 'y', 'z']
4. Inserting an element
• insert method is used
• Element is inserted at the position specified
• Syntax
<list name>.insert = (<position where list is to be inserted, ”new element”>)
• Example:
>>> list1.insert(3, "Element Inserted")
>>> print(list1)
['a', 'b', 'c', 'Element Inserted', 'x', 'y', 'z']
5. Removing an element
• remove method is used
• Only one value can be removed at a time
• Syntax
<list name>.remove = (<”element to be removed”>)
• Example:
>>> list1.remove("Element Inserted")
>>> print(list1)
['a', 'b', 'c', 'x', 'y', 'z']
What if there are 2 occurrences of the same
word?

In such case the FIRST OCCURRENCE of the


word is deleted and not the later one.
6. Arranging/sorting the elements
• sort method is used
• To sort a list the elements must be of same data type
• Syntax
<list name>.sort()
Example:
>>> listflowers = ['tulip','rose','marigold','daisy','lotus','jasmine’]
>>> print (listflowers)
['tulip', 'rose', 'marigold', 'daisy', 'lotus', 'jasmine’]

To sort in reverse order


>>>listflowers.sort(reverse=True)
7. Finding length of list
• Len() function is used
• Syntax
len(<list name>)
Example:
>>> listflowers = ['tulip','rose','marigold','daisy','lotus','jasmine’]
>>> l = len(listflowers)
>>> print(l)
6
8. Minimum & maximum of values in list
• min() and max() functions used
• Syntax
min(<listname>)
max(<listname>)
Example:
>>> marks = [45,47,48,40,41]
>>> low = min(marks)
>>> high = max(marks)
print (“Lowest = ” , low , “ and high = ” , high)
Lowest = 40 and high = 48
9. Finding sum of elements
• sum() function is used
• Syntax
sum(<listname>)

Example:
>>> marks = [45,47,48,40,41]
>>> total = sum(marks)
print (total)
221
10. count()
The count() method returns the number of times the specified
element appears in the list.
Syntax:
<listname>.count(<element to be counted>)

Example:
list1 = [2, 3, 45, 34, 54, 34, 67, 34, 22, 33, 34]
print(list1.count(34))
4
9. Removing an element
• pop method is used
• Using the pop method – Python displays the element that is deleted.
• Syntax
<listname>.pop(position)

Example:
>>> marks = [45,47,48,40,41]
>>> marks.pop(2)
48

Note: Pop () gives the element as output that is removed


10. Printing elements 1 by 1

• Use for loop

Example:
>>> for m in marks:
>>> print (m)
45
47
48
40
41
11. Copying a list

• Use copy()

Example:
>>> m = marks.copy()
>>> print (m)
[45, 47, 48, 40, 41]
in and not in operator

• Used to check if an element exists in the list

Example:
>>> languages = [“Python”, “Java”, “C++”, “Ruby”]
>>> print(“C++” in languages)
True
>>> print(“Kotlin” in languages)
False
>>> print(“Kotlin” not in languages)
True

You might also like