List, Tuples, Set And Dictnaray In Python

Lists are a fundamental data structure used to store collections of items. They are ordered, meaning elements have a defined sequence, and allow duplicates. Lists are versatile and can hold various data types like numbers, strings, Booleans, or even other lists.

Here's a breakdown of creating and working with lists in Python:


List, Tuples, Set And Dictnaray In Python


1. Creating Lists:

You can create lists using square brackets [] and separate elements with commas.


Example-


list= [12, 63, 28, 83, 85, 85, 96]


print(list)


Output- [12, 63, 28, 83, 85, 85, 96]


Use for  List Python-


Lists are ideal for storing any data type in a single variable, making it easy to manage groups of related information.

Example-


list = [46, 65, 56, 46, 56, 46]

for a in list:

 print(a)


Output-

46

65

56

46

56

46


(i)- append() Function


The append() method is a built-in function used with lists in Python. It allows you to add a single element to the end of an existing list. Here's a breakdown of how it works:

Syntax:

list.append(element)


Example-


list = [46,65,56,46,56,46]

list.append(100)

print(list)


Output -  [46,65,56,46,56,46,100]


(ii)- clear() Function


The clear() method is a built-in function used with lists in Python. It's designed to remove all elements from a list, effectively emptying it. Here's a detailed explanation of how clear() works:


Syntax

list.clear()


Example- 


list = [66,65,75,46,56,46]

list.clear()

print(list)


Output- Empty


(iii)- sort() Function


The sort() method is a built-in function used for sorting elements in a Python list. It modifies the original list in place, arranging the elements in ascending order by default. Here's a comprehensive explanation of how sort() works with lists:

Syntax:

list.sort()


Example-


list = [1, 56, 50, 3, 65,4]

list.sort()

print(list)


Output- [1, 3, 4, 50, 56, 65 ]


(iv)- reverse() Function


The reverse() method is a built-in function used with lists in Python. It reverses the order of elements in place, directly modifying the original list. Here's a breakdown of how reverse() works:

Syntax:

list.reverse()


Example-


list = [1, 76, 30, 3, 68,4]

list.reverse()

print(list)


Output- [76, 68, 30, 4, 3, 1]


(v)- remove() Function


The remove() method is a built-in function used with lists in Python to remove the first occurrence of a specified element from the list. It modifies the original list in place. Here's a detailed explanation of how remove() works:

Syntax:


list.remove()


Example-


list = [1, 54, 48, 3, 76,4]

list.remove(76)

print(list)


Output-  [1, 54, 48, 3,4]


(vi)- pop() Function


The pop() method is a built-in function used with lists in Python. It serves two purposes:

  • Removing an element: It removes an element at a specified position from the list and returns the removed element.
  • Returning the last element (default): If no index is specified, it removes and returns the last element from the list.

Here's a breakdown of how pop() works in both scenarios:


Syntax:


list.pop()


Example-


list = [1, 34, 38, 3, 86,4]

list.pop(2)     #(2) index value (0-n)

print(list)


Output -  [1, 34, 3, 86,4]


(vii)- insert() Function 


The insert() method is a built-in function used with lists in Python to insert an element at a specified position within the list. It modifies the original list in place, meaning it directly alters the existing list.

Here's a detailed explanation of how insert() works with lists:


Syntax:

list.insert()


Example-


list = [1, 24, 48, 3, 66,4]

list.insert(2, 40)   #(index,value)

print(list)


Output -  [1, 24, 40, 48, 3, 66,4]


(viii)- index() Function


The index() method is a built-in function used with lists in Python. It searches the list for a specified element and returns the lowest index at which that element appears in the list. If the element is not found, it raises a ValueError.

Here's a breakdown of how index() works with lists:


Syntax:


list.index()


Example-


list= [21, 32, 22, 32, 42]

print(list.index(42)) 


(ix)- copy() Function 


The copy() method is a built-in function used with lists in Python. It creates a shallow copy of a list. Here's a detailed explanation of how it works:

Syntax-


list.copy()


Example-


list= [21, 32, 22, 32, 42]

list2 = list.copy()

print(list2)


Slicing Operator in List


List slicing is a powerful technique for extracting a subset of elements from a list. It uses a concise syntax to create a new list containing a specific portion of the original list. Here's a breakdown of how list slicing works:


Syntax:


[start : stop: step]


Example-


Passive Slicing in List -


1- list= [21, 32, 22, 32, 42]

print(list[2]) # index value[2]


Output - [22]


2 - list= [21, 32, 22, 32, 42]

print(list[2:4]) # index value


Output - [22, 32]


3- list= [21, 82, 22, 32, 42]

print(list[1:4:2])


Output - [82, 32]



Negative Slicing in List


1 - list= [21, 82, 22, 32, 42]

print(list[-2])


Output - [ 32 ]



2 - list= [21, 82, 22, 32, 42]

print(list[-0 :-2])


Output - [ 21, 82, 22 ]



3 - list= [21, 82, 22, 32, 42]

print(list[-1 :-1: -1])


Output - [  ] - Empty



Tuple in Python -


tuples are another fundamental data structure used to store collections of items. Unlike lists, tuples are immutable, meaning their elements cannot be changed after creation. Here's a comparison of lists and tuples:

Lists:

  1. Mutable (elements can be modified)
  2. Enclosed in square brackets []
  3. Well-suited for dynamic data where you need to add, remove, or change elements

Tuples:

  1. Immutable (elements cannot be modified)
  2. Enclosed in parentheses ()
  3. Ideal for representing fixed data sets or collections that shouldn't be altered


tup = ( 21, 31, 32, 54, 75, 43)

print(tup)


Output - ( 21, 31, 32, 54, 75, 43)


(i) - count()


You can't use the count() method directly on a tuple in Python to count the occurrences of an element. Tuples are immutable, meaning their elements cannot be changed after creation and count() are not designed to work with immutable sequences.


Syntax-

tup = count()


Example-

tup = ( 21, 31, 32, 54, 21, 75, 43,21)

a = 21

print(tup.count(a))


Output - 3    # 21 three times 


(ii) - index()

index() method with tuples in Python to find the index of the first occurrence of a specified element. It works similarly to how it works with lists. Here's a breakdown of how index() is used with tuples:


Syntax:

tup.index()


Example-


tup = ( 21, 31, 32, 54, 21, 75, 43,21)


a = 75

print(tup.index(a))


Output - 5   - 75 is index number 5


Set  In Python-


Python, sets are unordered collections of unique elements. They are defined using curly braces {} and offer distinct functionalities compared to lists and tuples. Here's a detailed explanation of sets in Python:


Characteristics:


  1. Unordered: Elements in a set don't have a specific order, and you cannot access them using indexing.
  2. Unique Elements: Sets can only contain unique elements. Duplicate values are not allowed.
  3. Mutable: Sets are mutable, meaning you can add or remove elements after creation.


Common Set Operations:


  • Union (|): Creates a new set containing elements from both sets.
  • Intersection (&): Creates a new set containing elements present in both sets.
  • Difference (-): Creates a new set containing elements in the first set but not in the second.
  1. in: Checks if an element is present in the set.
  2. add(): Adds an element to the set.
  3. remove(): Removes a specific element from the set.
  4. discard(): Attempts to remove an element from the set (raises no error if the element is not present).
  5. pop(): Removes and returns an arbitrary element from the set.


By understanding sets and their functionalities, you can effectively manage collections of unique data and perform operations specific to set theory in your Python programs.


(i) - union() set1 = {1,4,5,6,8} set2 = {2,5,4,7,3} set3 = set1.union(set2) print(set3) Output - { 1,2,3,45,6,7,8} (ii) - intersection() set1 = {1,4,5,6,8} set2 = {2,5,4,7,3} set3 = set1.intersection(set2) print(set3) Output - { 5,4} (iii) - discard() set1 = {1,4,5,6,8} set1.discard(1) print(set1) Output - { 4,5,6,8} (iv) - remove() set1 = {1,4,5,6,8} set1.remove(5) print(set1) Output - {1,4,6,8} (v) - issubset() d1 = { 21,54,21,56} d2 = {54,21} d3= d1.issubset(d2) print(d3) Output - False d1 = { 21,54,21,56} d2 = {54,21} d3= d2.issubset(d1) print(d3) Output - True (vi) - issuperset() d1 = { 21,54,21,56} d2 = {54,21} d3= d2.issubset(d1) print(d3) Output - False d1 = { 21,54,21,56} d2 = {54,21} d3= d1.issuperset(d2) print(d3) Output - True

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.