BData Structure
BData Structure
Data Structures
Prepared by:
Shweta Dhareshwar
Department of MCA
Lists
• Updating List – We can update the existing item of the list with the help
of the index.
• Deleting list Item- With the help of del and list index, we can delete any
item by specifying its index.
• Addition of the List – Two lists can be added using the + operator.
• Multiplication of List -
Built-In Functions Used on Lists
• len()-Python list method len() returns the number of elements in the list.
• Max() - max returns the elements from the list with maximum value.
• Min() - min() returns the elements from the list with minimum value.
List Methods
Python List provides different methods to add items to a list.
append() - The append() method adds an item at the end of the list.
For example:
numbers = [21, 34, 54, 12]
print("Before Append:", numbers)
# using append method
numbers.append(32)
print("After Append:", numbers)
• extend()- We use the extend() method to add all items of one list to another.
List.extend(sequence)
For example:
prime_numbers = [2, 3, 5]
print("List1:", prime_numbers)
even_numbers = [4, 6, 8]
print("List2:", even_numbers)
# join two lists
prime_numbers.extend(even_numbers)
print("List after extend:", prime_numbers)
• Clear() - removes all items from the list.
Example :
prime_numbers = [2, 3, 5]
prime_numbers.clear()
• Copy() - returns the shallow copy of the list.
• list.copy()
prime_numbers = [2, 3, 5]
prime_numbers1=prime_numbers.copy()
• count()- returns the count of the specified item in the list.
• list.count(item)
prime_numbers = [2, 3, 5]
prime_numbers.count (2)
• index()- returns the index of the first matched item
list.index(item)
prime_numbers = [2, 3, 5]
X= prime_numbers.index(2)
• insert() -inserts an item at the specified index
prime_numbers = [2, 3, 5]
prime_numbers.insert (2,8)
• pop() - returns and removes item present at the given index
prime_numbers = [2, 3, 5]
prime_numbers.pop(2)
• reverse()- reverses the item of the list.
prime_numbers.reverse()
print(prime_numbers)
• sort() - sort the list in ascending/descending order.
prime_numbers.sort()
print(prime_numbers)
• Join () - There are several ways to join, or concatenate, two or more lists in Python.
Example:
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
List 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 item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
Negative Indexing
Specify negative indexes if you want to start the search from the end of the list:
Example :
This example returns the items from "orange" (-4) to, but NOT including
"mango" (-1):
Strings
String Length:To get the length of a string, use the len() function.
Ex: str=“Good Morning”
print(len(str))
Check String : To check if a certain phrase or character is
present in a string, we can use the keyword in.
Ex: txt = "The best things in life are free!"
print("free" in txt)
Check if NOT: To check if a certain phrase or character is
NOT present in a string, we can use the keyword not in.
Ex: txt = "The best things in life are free!"
print(“sky" not in txt)
String Slicing
• To determine how many items a tuple has, use the len() method
Format strings
• It is also known as string interpolation or string formatting, are a way to dynamically insert values into a string in
Python. They allow you to create formatted output by specifying placeholders within a string and providing the
values to replace those placeholders.
• In Python, there are several ways to format strings, but one commonly used method is using the .format() method.
Here's an example:
name = “Amar"
age = 25
print("My name is {} and I am {} years old.".format(name, age))
In this example, the string "My name is {} and I am {} years old." contains two placeholders {}. The .format() method is
called on the string and the values to be inserted are provided as arguments within the parentheses. The values "Amar"
and 25 are passed in the same order as the placeholders, and they replace the placeholders in the resulting output.
• You can also specify the order of the values explicitly by using numbers
within the curly braces. For example:
name = "Amar"
age = 25
print("My name is {0} and I am {1} years old. {0}!".format(name, age))
In this case, {0} refers to the first value (name), and {1} refers to the second
value (age). By repeating {0}, we can use the same value multiple times within
the string.
Dictionary
• Dictionary it is a data structure , which contains key and value pairs. It is
written as a sequence of the key/value or item separated by commas.
• Eg: dict1 = { "brand": "Ford", "model": "Mustang", "year": 1964}