0% found this document useful (0 votes)
126 views22 pages

List, Tuple, Set, Dictionary-7

The document discusses lists in Python. Lists are mutable collections that can hold different data types and are indexed. Common list operations include accessing elements, repetition, concatenation, length, iteration and membership testing. The document also covers tuples which are immutable lists, sets which contain unique elements, and dictionaries which contain key-value pairs.

Uploaded by

ppawah04
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)
126 views22 pages

List, Tuple, Set, Dictionary-7

The document discusses lists in Python. Lists are mutable collections that can hold different data types and are indexed. Common list operations include accessing elements, repetition, concatenation, length, iteration and membership testing. The document also covers tuples which are immutable lists, sets which contain unique elements, and dictionaries which contain key-value pairs.

Uploaded by

ppawah04
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/ 22

List, tuple, set, dictionary

List
● A list is a collection of different kinds of values or items.
● Python lists are mutable i.e. we can change their elements after forming.
● [ ] are used to enclose the list items
● The comma (,) serve as separators.
● The lists items are in order.
● The list element can be accessed via the index.
List operations

Repetition , Concatenation , Membership, Iteration, length are


the five basic operation that can be performed on any
sequence
List operations : repetition
List operations : concatenation
length
Iteration :The for loop is used to iterate over the list
elements.
Membership : It returns true if a particular item exists in a
particular list otherwise false.
Accessing list items
List items are indexed and you can access them by referring to the index number

-1 refers to the last item, -2 refers to the second last item etc
List Index
● Return the third, fourth, and fifth item:
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])
● By leaving out the start value, the range will start at the first item:
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[:4])
● By leaving out the end value, the range will go on to the end of the list:
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:])
Adding element to the list : append()
1. l=[]
2. n = int(input("Enter the number of elements in the list:"))
3. # for loop to take the input
4. for i in range(0,n):
5. l.append(input("Enter the item:"))
6. print("printing the list items..")
7.

8. # traversal loop to print the list items


9.

10. for i in l:
11. print(i, end = " ")
remove() : removing the lament from the list

1. list = [0,1,2,3,4]
2. print("printing original list: ");
3. for i in list:
4. print(i,end=" ")
5. list.remove(2)
6. print("\nprinting the list after the removal of first element...")
7. for i in list:
8. print(i,end=" ")
Len () , max() , min()
1. list1 = [12, 16, 18, 20, 39, 40]
2. # finding length of the list
3. len(list1)
4.

5. # maximum of the list


6. list1 = [103, 675, 321, 782, 200]
7. print(max(list1))
8.

9. # minimum of the list


10. list1 = [103, 675, 321, 782, 200]
11. print(min(list1))
List methods
list.del() : delete 1st element front the list
List.reverse() : retrieve the element of list in reverse order

Cloning of list using slicing operator


Y=X[:]
Copy list
Y = x.copy()
Find the common elements in two lists
Set1 = interaction(set2)
List methods

List.sum() : Return the sum of all elements of the list

list.insert(i , x) : insert the element in the list at specified index

list.extend(list1) : appent the new list

list.count(x) : return the occurrences of x in list

list.pop() : Remove the last element from the list

list.clear() : delete all elements from the list

list.sort() : sort the element of the list


List comprehension
Creation of new list from an iterable object ( like list, set, tuple, dictionary
etc) that satisfy the given condition

Sqaress = [ ]
For x in range (1,11);
squares.append(x**2)
Print squares

List comprehension

Squares = [ x**2 ] for x in range (1,11)


Tuples
● Immutable list of elements
● Used to store the data that is not intended for modification
● () is used to create the tuple
● Tuple may have different type of elements
● Tup = 1,2,3,4 elements written without braces and separated by
command are treated as tuple by default
Tup = () : empty tuple
Tup = (10,) : tuple with one element
tup = tuple(list) : convert list into tuple
Tup = tuple (range ( 4,9,2) creating tuple using range
Accessing the elements of tuple
Tup[2] : accessing the 3rd element of tuple

Tup [-1] : accessing the last element

Tup[ : ] : retrieving all elements of tuple


Functions of tuples
len(tp) : return the number of elements in tuple

min(tp) : return the smallest element in tuple

max(tp) : return the largest element in tuple

tp. count(x) : return the occurrence of element x in tuple

tp.index(x) : return first occurrence of element x

sorted(tp , reverse = true) : sort the elements based on value passed to reverse
parameter
Dictionary
● Represents group of elements arranged in key value : pair
● Key : value is inserted using { }
● Duplicate keys are not allowed
● Keys are immutable

Dic = {‘name’ : ‘chander’, ‘id’ : 102, ‘salary’ : 10000}

Len (dict) gives length of dictionary

Dic[‘key’] = value gives new value to the key

Del dic[‘id’] deletes the key: value pair of dictionary


dictionary
dict. clear() : remves all elements of dictionary
D1 = d.copy() : copies all elements of d to d1
d.fromkeys(s,[ ,v]): creates new dictionary with the keys from sequence ‘s’ and
values all set to v
d.get(k [,v]) return value associated with key ‘k’
d.item() : return tuple containing key value pair
d.keys() : return sequence of all keys
d.values() : return sequence of values.
d.update(x) : add all elements of dictionary ‘x’ to ‘d’
d.pop(k,[,v]) : removes the key ‘k’ and its value from ‘d’ and returns the value
Converting list into dictionaries
Zip () : take two sequence as parameter and return one zip object , which
need to be converted into dictionary object

Countries = [‘USA’ ,’india’ , germany’ , france’ ]

Cities = [‘washington’ , ‘delhi’ , ‘berlin’, ‘paris’ ]

Z = zip (countries, cities) convert the sequence into zip object

D = dict(z)

You might also like