0% found this document useful (0 votes)
6 views13 pages

Python Data Structures

The document provides an overview of Python data structures, specifically lists, dictionaries, and sets, detailing their methods and functionalities. Each section includes descriptions and code examples for various operations such as adding, modifying, and accessing elements. The content serves as a reference for understanding and utilizing these built-in data types in Python programming.

Uploaded by

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

Python Data Structures

The document provides an overview of Python data structures, specifically lists, dictionaries, and sets, detailing their methods and functionalities. Each section includes descriptions and code examples for various operations such as adding, modifying, and accessing elements. The content serves as a reference for understanding and utilizing these built-in data types in Python programming.

Uploaded by

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

Python Data Structures

List
Package Description Code Example
/Method
append() The `append()` method is used Syntax:
to add an element to the end of 1. 1
a list. 1. list_name.append(element)
Copied!
Example:
1. 1
2. 2
1. fruits = ["apple", "banana", "orange"]
2. fruits.append("mango") print(fruits)
Copied!
copy() The `copy()` method is used to Example 1:
create a shallow copy of a list. 1. 1
2. 2
3. 3
1. my_list = [1, 2, 3, 4, 5]
2. new_list = my_list.copy() print(new_list)
3. # Output: [1, 2, 3, 4, 5]
Copied!
count() The `count()` method is used to Example:
count the number of 1. 1
occurrences of a specific 2. 2
element in a list in Python. 3. 3
1. my_list = [1, 2, 2, 3, 4, 2, 5, 2]
2. count = my_list.count(2) print(count)
3. # Output: 4
Copied!
Creating a A list is a built-in data type that Example:
list represents an ordered and 1. 1
mutable collection of elements. 1. fruits = ["apple", "banana", "orange", "mango"]
Lists are enclosed in square Copied!
brackets [] and elements are
separated by commas.
del The `del` statement is used to Example:
remove an element from list. 1. 1
`del` statement removes the 2. 2
element at the specified index. 3. 3
1. my_list = [10, 20, 30, 40, 50]
2. del my_list[2] # Removes the element at index 2
print(my_list)
3. # Output: [10, 20, 40, 50]
Copied!
extend() The `extend()` method is used Syntax:
to add multiple elements to a 1. 1
list. It takes an iterable (such as 1. list_name.extend(iterable)
another list, tuple, or string) Copied!
and appends each element of Example:
the iterable to the original list. 1. 1
2. 2
3. 3
4. 4
1. fruits = ["apple", "banana", "orange"]
2. more_fruits = ["mango", "grape"]
3. fruits.extend(more_fruits)
4. print(fruits)
Copied!
Indexing Indexing in a list allows you to Example:
access individual elements by 1. 1
their position. In Python, 2. 2
indexing starts from 0 for the 3. 3
first element and goes up to 4. 4
`length_of_list - 1`. 5. 5
1. my_list = [10, 20, 30, 40, 50]
2. print(my_list[0])
3. # Output: 10 (accessing the first element)
4. print(my_list[-1])
5. # Output: 50 (accessing the last element using
negative indexing)
Copied!
insert() The `insert()` method is used to Syntax:
insert an element. 1. 1
1. list_name.insert(index, element)
Copied!
Example:
1. 1
2. 2
3. 3
1. my_list = [1, 2, 3, 4, 5]
2. my_list.insert(2, 6)
3. print(my_list)
Copied!
Modifying a You can use indexing to modify Example:
list or assign new values to specific 1. 1
elements in the list. 2. 2
3. 3
4. 4
1. my_list = [10, 20, 30, 40, 50]
2. my_list[1] = 25 # Modifying the second element
3. print(my_list)
4. # Output: [10, 25, 30, 40, 50]
Copied!
pop() `pop()` method is another way Example 1:
to remove an element from a 1. 1
list in Python. It removes and 2. 2
returns the element at the 3. 3
specified index. If you don't 4. 4
provide an index to the `pop()` 5. 5
method, it will remove and 6. 6
return the last element of the 7. 7
list by default 1. my_list = [10, 20, 30, 40, 50]
2. removed_element = my_list.pop(2) # Removes and
returns the element at index 2
3. print(removed_element)
4. # Output: 30
5.
6. print(my_list)
7. # Output: [10, 20, 40, 50]
Copied!
Example 2:
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
1. my_list = [10, 20, 30, 40, 50]
2. removed_element = my_list.pop() # Removes and
returns the last element
3. print(removed_element)
4. # Output: 50
5.
6. print(my_list)
7. # Output: [10, 20, 30, 40]
Copied!
remove() To remove an element from a Example:
list. The `remove()` method 1. 1
removes the first occurrence of 2. 2
the specified value. 3. 3
4. 4
1. my_list = [10, 20, 30, 40, 50]
2. my_list.remove(30) # Removes the element 30
3. print(my_list)
4. # Output: [10, 20, 40, 50]
Copied!
reverse() The `reverse()` method is used Example 1:
to reverse the order of 1. 1
elements in a list 2. 2
3. 3
1. my_list = [1, 2, 3, 4, 5]
2. my_list.reverse() print(my_list)
3. # Output: [5, 4, 3, 2, 1]
Copied!
Slicing You can use slicing to access a Syntax:
range of elements from a list. 1. 1
1. list_name[start:end:step]
Copied!
Example:
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
11. 11
12. 12
1. my_list = [1, 2, 3, 4, 5]
2. print(my_list[1:4])
3. # Output: [2, 3, 4] (elements from index 1 to 3)
4.
5. print(my_list[:3])
6. # Output: [1, 2, 3] (elements from the beginning up to
index 2)
7.
8. print(my_list[2:])
9. # Output: [3, 4, 5] (elements from index 2 to the end)
10.
11. print(my_list[::2])
12. # Output: [1, 3, 5] (every second element)
Copied!
sort() The `sort()` method is used to Example 1:
sort the elements of a list in 1. 1
ascending order. If you want to 2. 2
sort the list in descending order, 3. 3
you can pass the `reverse=True` 4. 4
argument to the `sort()` 1. my_list = [5, 2, 8, 1, 9]
method. 2. my_list.sort()
3. print(my_list)
4. # Output: [1, 2, 5, 8, 9]
Copied!
Example 2:
1. 1
2. 2
3. 3
4. 4
1. my_list = [5, 2, 8, 1, 9]
2. my_list.sort(reverse=True)
3. print(my_list)
4. # Output: [9, 8, 5, 2, 1]
Copied!
Dictionary
Package/ Description Code Example
Method

Accessing You can access the values in a Syntax:


Values dictionary using their corresponding
1. 1
`keys`.
1. Value = dict_name["key_name"]

Copied!

Example:

1. 1

2. 2

1. name = person["name"]

2. age = person["age"]

Copied!

Add or modify Inserts a new key-value pair into the Syntax:


dictionary. If the key already exists,
1. 1
the value will be updated; otherwise,
a new entry is created. 1. dict_name[key] = value

Copied!

Example:

1. 1

2. 2

1. person["Country"] = "USA" # A new


entry will be created.

2. person["city"] = "Chicago" #
Update the existing value for the
same key

Copied!

clear() The `clear()` method empties the Syntax:


dictionary, removing all key-value
1. 1
pairs within it. After this operation,
the dictionary is still accessible and 1. dict_name.clear()
can be used further.
Copied!

Example:

1. 1

1. grades.clear()

Copied!

copy() Creates a shallow copy of the Syntax:


dictionary. The new dictionary
1. 1
contains the same key-value pairs as
the original, but they remain distinct 1. new_dict = dict_name.copy()
objects in memory.
Copied!

Example:

1. 1

2. 2

1. new_person = person.copy()

2. new_person = dict(person) #
another way to create a copy of
dictionary

Copied!

Creating a A dictionary is a built-in data type that Example:


Dictionary represents a collection of key-value
1. 1
pairs. Dictionaries are enclosed in
curly braces `{}`. 2. 2

1. dict_name = {} #Creates an empty


dictionary

2. person = { "name": "John", "age":


30, "city": "New York"}

Copied!

del Removes the specified key-value pair Syntax:


from the dictionary. Raises a
1. 1
`KeyError` if the key does not exist.
1. del dict_name[key]

Copied!

Example:
1. 1

1. del person["Country"]

Copied!

items() Retrieves all key-value pairs as tuples Syntax:


and converts them into a list of tuples.
1. 1
Each tuple consists of a key and its
corresponding value. 1. items_list = list(dict_name.items())

Copied!

Example:

1. 1

1. info = list(person.items())

Copied!

key existence You can check for the existence of a Example:


key in a dictionary using the `in`
1. 1
keyword
2. 2

1. if "name" in person:

2. print("Name exists in the


dictionary.")

Copied!

keys() Retrieves all keys from the dictionary Syntax:


and converts them into a list. Useful
1. 1
for iterating or processing keys using
list methods. 1. keys_list = list(dict_name.keys())

Copied!

Example:

1. 1

1. person_keys = list(person.keys())

Copied!

update() The `update()` method merges the Syntax:


provided dictionary into the existing
1. 1
dictionary, adding or updating key-
value pairs. 1. dict_name.update({key: value})
Copied!

Example:

1. 1

1. person.update({"Profession":
"Doctor"})

Copied!

values() Extracts all values from the dictionary Syntax:


and converts them into a list. This list
1. 1
can be used for further processing or
analysis. 1. values_list =
list(dict_name.values())

Copied!

Example:

1. 1

1. person_values =
list(person.values())

Copied!
Sets
Package/ Description Code Example
Method

add() Elements can be added to a set Syntax:


using the `add()` method.
1. 1
Duplicates are automatically
removed, as sets only store 1. set_name.add(element)
unique values.
Copied!

Example:

1. 1

1. fruits.add("mango")

Copied!

clear() The `clear()` method removes all Syntax:


elements from the set, resulting
1. 1
in an empty set. It updates the
set in-place. 1. set_name.clear()

Copied!

Example:

1. 1

1. fruits.clear()

Copied!

copy() The `copy()` method creates a Syntax:


shallow copy of the set. Any
1. 1
modifications to the copy won't
affect the original set. 1. new_set = set_name.copy()

Copied!

Example:

1. 1
1. new_fruits = fruits.copy()

Copied!

Defining Sets A set is an unordered collection Example:


of unique elements. Sets are
1. 1
enclosed in curly braces `{}`.
They are useful for storing 2. 2
distinct values and performing
set operations. 1. empty_set = set() #Creating an Empty
Set

2. fruits = {"apple", "banana", "orange"}

Copied!

discard() Use the `discard()` method to Syntax:


remove a specific element from
1. 1
the set. Ignores if the element is
not found. 1. set_name.discard(element)

Copied!

Example:

1. 1

1. fruits.discard("apple")

Copied!

issubset() The `issubset()` method checks if Syntax:


the current set is a subset of
1. 1
another set. It returns True if all
elements of the current set are 1. is_subset = set1.issubset(set2)
present in the other set,
otherwise False. Copied!

Example:

1. 1

1. is_subset = fruits.issubset(colors)

Copied!

issuperset() The `issuperset()` method checks Syntax:


if the current set is a superset of
1. 1
another set. It returns True if all
elements of the other set are 1. is_superset = set1.issuperset(set2)
present in the current set,
Copied!
otherwise False. Example:

1. 1

1. is_superset = colors.issuperset(fruits)

Copied!

pop() The `pop()` method removes and Syntax:


returns an arbitrary element
1. 1
from the set. It raises a
`KeyError` if the set is empty. 1. removed_element = set_name.pop()
Use this method to remove
elements when the order Copied!
doesn't matter. Example:

1. 1

1. removed_fruit = fruits.pop()

Copied!

remove() Use the `remove()` method to Syntax:


remove a specific element from
1. 1
the set. Raises a `KeyError` if the
element is not found. 1. set_name.remove(element)

Copied!

Example:

1. 1

1. fruits.remove("banana")

Copied!

Set Operations Perform various operations on Syntax:


sets: `union`, `intersection`,
1. 1
`difference`, `symmetric
difference`. 2. 2

3. 3

4. 4

1. union_set = set1.union(set2)

2. intersection_set =
set1.intersection(set2)

3. difference_set = set1.difference(set2)
4. sym_diff_set =
set1.symmetric_difference(set2)

Copied!

Example:

1. 1

2. 2

3. 3

4. 4

1. combined = fruits.union(colors)

2. common = fruits.intersection(colors)

3. unique_to_fruits =
fruits.difference(colors)

4. sym_diff =
fruits.symmetric_difference(colors)

Copied!

update() The `update()` method adds Syntax:


elements from another iterable
1. 1
into the set. It maintains the
uniqueness of elements. 1. set_name.update(iterable)

Copied!

Example:

1. 1

1. fruits.update(["kiwi", "grape"]

You might also like