Docsity

Python Data Structures: Lists, Tuples, and Dictionaries, Lecture notes of Software Engineering

Partial preview of the text

Download Python Data Structures: Lists, Tuples, and Dictionaries and more Lecture notes Software Engineering in PDF only on Docsity!

Lists, Tuples and

Dictionaries

P.Raja,

Assistant professor in ES(D4),

School of Electronics and Electrical

Engineering

Outline

  • (^) Definition, Declaration, accesses of elements,

slicing, methods, comprehension & examples.

  • (^) List
  • (^) Tuple
  • (^) Dictionary

List

  • (^) List Items - Data Types :-

list1 = ["apple", "banana", "cherry"]

list2 = [ 1 , 5 , 7 , 9 , 3 ]

list3 = [True, False, False]

list1 = ["abc", 34 , True, 40 , "male"]

  • (^) The list() Constructor :-

list =

list(("apple", "banana", "cherry")) # note

the double round-brackets

print(list)

List

  • (^) Python Collections (Arrays) :-
  • (^) List is a collection which is ordered and changeable.
  • (^) Tuple is a collection which is ordered and

unchangeable.

  • (^) Both list and tuple Allows duplicate members.
  • (^) Set is a collection which is unordered, unchangeable*,

and unindexed.

  • (^) Dictionary is a collection which is ordered** and

changeable.

  • (^) Both Set and Dictionary do not allow duplicate

members.

List

  • (^) Access Items :-
  • (^) **+, ***

List

  • (^) Check if Item Exists :-

list = ["apple", "banana", "cherry"]

if "apple" in list:

print("Yes, 'apple' is in the fruits list")

  • (^) Change Item Value :-

list = ["apple", "banana", "cherry"]

list[ 1 ] = “orange"

print(list)

  • (^) Change a Range of Item Value :-

list =

["apple", "banana", "cherry", "orange", "kiwi", "mango"]

list[ 1 : 3 ] = ["blackcurrant", "watermelon"]

print(list)

List

Ordered Vs Unordered

List- List/Array Methods

Method Description

append() Adds an element at the end of the list

clear() Removes all the elements from the list

copy() Returns a copy of the list

count() Returns the number of elements with the specified value

extend() Add the elements of a list (or any iterable), to the end of the current list

index() Returns the index of the first element with the specified value

insert() Adds an element at the specified position

pop() Removes the element at the specified position

remove() Removes the first item with the specified value

reverse() Reverses the order of the list

sort() Sorts the list

List- List/Array Methods

append() Method :-

list .append( elmnt )

List- List/Array Methods

copy() Method :-

list .copy()

List- List/Array Methods

count() Method :-

list .count( value )

List- List/Array Methods

index() Method :-

list .index( elmnt )

List- List/Array Methods

insert() Method :-

list .insert( pos , elmnt )