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

Python List and Tuple Operations Guide

The document provides a series of Python code snippets demonstrating various operations on lists and tuples, including creating, modifying, and accessing elements. Key operations include adding and removing elements, sorting, finding maximum and minimum values, and checking for existence. It also covers tuple operations such as concatenation and element access.
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 views7 pages

Python List and Tuple Operations Guide

The document provides a series of Python code snippets demonstrating various operations on lists and tuples, including creating, modifying, and accessing elements. Key operations include adding and removing elements, sorting, finding maximum and minimum values, and checking for existence. It also covers tuple operations such as concatenation and element access.
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

1.

Create a List and Print All Elements :


my_list = [1, 2, 3, 4, 5]
print("List elements:", my_list)

o/p:
List elements: [1, 2, 3, 4, 5]

2. Find the Length of a List:


my_list = [1, 2, 3, 4, 5]
print("Length of the list:", len(my_list))

o/p:
Length of the list: 5

3. Add an Element to a List:


my_list = [1, 2, 3]
my_list.append(4)
print("List after adding an element:", my_list)

o/p:
List after adding an element: [1, 2, 3, 4]

4. Remove an Element from a List:


my_list = [1, 2, 3, 4]
my_list.remove(3)
print("List after removing an element:", my_list)

o/p:
List after removing an element: [1, 2, 4]
5. Sort a List in Ascending Order:
my_list = [4, 2, 1, 3]
my_list.sort()
print("Sorted list in ascending order:", my_list)

o/p:
Sorted list in ascending order: [1, 2, 3, 4]

6. Sort a List in Descending Order:


my_list = [4, 2, 1, 3]
my_list.sort(reverse=True)
print("Sorted list in descending order:", my_list)

o/p:
Sorted list in descending order: [4, 3, 2, 1]

7. Find the Maximum Element in a List:


my_list = [4, 2, 1, 3]
print("Maximum element:", max(my_list))

o/p:
Maximum element: 4

8. Find the Minimum Element in a List:


my_list = [4, 2, 1, 3]
print("Minimum element:", min(my_list))

o/p:
Minimum element: 1
9. Reverse a List:
my_list = [1, 2, 3, 4]
my_list.reverse()
print("Reversed list:", my_list)

o/p:
Reversed list: [4, 3, 2, 1]

10. Count Occurrences of an Element in a List:


my_list = [1, 2, 2, 3, 2, 4]
print("Occurrences of 2:", my_list.count(2))

o/p:
Occurrences of 2: 3

11. Find the Index of an Element in a List:


my_list = [1, 2, 3, 4]
print("Index of 3:", my_list.index(3))

o/p:
Index of 3: 2

12. Check if an Element Exists in a List:


my_list = [1, 2, 3, 4]
print("Does 3 exist in the list?", 3 in my_list)

o/p:
Does 3 exist in the list? True

13. Concatenate Two Lists:


list1 = [1, 2, 3]
list2 = [4, 5, 6]
result = list1 + list2
print("Concatenated list:", result)

o/p:
Concatenated list: [1, 2, 3, 4, 5, 6]

14. Find the Sum of All Elements in a List:


my_list = [1, 2, 3, 4]
print("Sum of elements:", sum(my_list))

o/p:
Sum of elements: 10

15. Multiply All Elements in a List:


from functools import reduce
my_list = [1, 2, 3, 4]
product = reduce(lambda x, y: x * y, my_list)
print("Product of elements:", product)

o/p:
Product of elements: 24

16. Remove Duplicates from a List:


my_list = [1, 2, 2, 3, 4, 4, 5]
unique_list = list(set(my_list))
print("List without duplicates:", unique_list)

o/p:
List without duplicates: [1, 2, 3, 4, 5]

17. Find Even Numbers in a List:


my_list = [1, 2, 3, 4, 5, 6]
even_numbers = [x for x in my_list if x % 2 == 0]
print("Even numbers:", even_numbers)

o/p:
Even numbers: [2, 4, 6]

18. Find Odd Numbers in a List:


my_list = [1, 2, 3, 4, 5, 6]
odd_numbers = [x for x in my_list if x % 2 != 0]
print("Odd numbers:", odd_numbers)

o/p:
Odd numbers: [1, 3, 5]

19. Create a List of Squares:


numbers = [1, 2, 3, 4, 5]
squares = [x ** 2 for x in numbers]
print("Squares:", squares)

o/p:
Squares: [1, 4, 9, 16, 25]

20. Split a List into Two Halves:


my_list = [1, 2, 3, 4, 5, 6]
mid = len(my_list) // 2
first_half = my_list[:mid]
second_half = my_list[mid:]
print("First half:", first_half)
print("Second half:", second_half)

o/p:
First half: [1, 2, 3]
Second half: [4, 5, 6]

21. Create a Tuple and Print All Elements:


my_tuple = (1, 2, 3, 4, 5)
print("Tuple elements:", my_tuple)

o/p:
Tuple elements: (1, 2, 3, 4, 5)

22. Find the Length of a Tuple:


my_tuple = (1, 2, 3, 4, 5)
print("Length of the tuple:", len(my_tuple))

o/p:
Length of the tuple: 5

23. Access Elements of a Tuple:


my_tuple = (1, 2, 3, 4, 5)
print("First element:", my_tuple[0])
print("Last element:", my_tuple[-1])

o/p:
First element: 1
Last element: 5
24. Check if an Element Exists in a Tuple:
my_tuple = (1, 2, 3, 4, 5)
print("Does 3 exist in the tuple?", 3 in my_tuple)

o/p:
Does 3 exist in the tuple? True

25. Concatenate Two Tuples:


tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
result = tuple1 + tuple2
print("Concatenated tuple:", result)

o/p:
Concatenated tuple: (1, 2, 3, 4, 5, 6)

You might also like