New Batch Starts on 16th Oct.
Module: Network Fundamentals (L2 L3 Protocol Testing)
Timing: 07:00 AM to 08:00 AM IST (Weekdays)
Sign Up Now !

Chapter 8: Python Lists and Tuples -Operations and Methods

Working with Lists and Tuples in Python

In this chapter, we’ll explore two important data structures in Python: lists and tuples. Lists allow you to store a collection of items that can be modified, while tuples are immutable, meaning their elements cannot be changed. In this chapter, you’ll learn about basic list operations like slicing, appending, and removing elements, as well as useful list methods like count(), sort(), and reverse(). We’ll also cover tuples and their key features, such as concatenation and indexing. By the end of this chapter, you should have a solid understanding of how to work with lists and tuples in Python.

Basic List Operations in Python

Explanation:

  • len(a) returns the number of elements in the list.
  • min(a) and max(a) return the smallest and largest elements in the list, respectively.
  • a + [100, 110, 120] concatenates another list to a without modifying the original list.
  • You can access elements by index (e.g., a[0]) and modify them (e.g., a[0] = 15).
  • del a[2] removes the element at index 2.

List Slicing in Python

Explanation:

  • The syntax a[x:y:z] slices the list from index x to y (exclusive), stepping by z.
  • Positive z slices from left to right, while negative z slices from right to left.

Lists Inside Lists

Explanation:

  • You can nest lists inside other lists and access them by indexing, allowing for multi-dimensional list structures.

List Methods in Python

Explanation:

  • count() counts the occurrences of an element in the list.
  • append() adds a single element to the end of the list.
  • extend() adds elements of another list.
  • pop() removes and returns the last element (or element at a specific index).
  • remove() removes the first occurrence of the specified value.
  • insert() inserts an element at a specified index.

List Sorting and Reversing

Explanation:

  • sort() sorts the list in ascending order by default, or descending order when reverse=True.
  • reverse() reverses the order of the elements in the list.

Tuple Operations in Python

Explanation:

  • Tuples are similar to lists but immutable, meaning their elements cannot be modified once created.
  • count() and index() work similarly to their list counterparts.
  • You can concatenate tuples and repeat them using the + and * operators.

Programming Exercises :

  1. Write a Python program to find the sum of all numbers in a list.
  2. Write a Python program to find the largest number in a given list without using max().
  3. Write a Python program to find the common numbers between two lists.
  4. Write a Python program to print all even numbers from a given list.
  5. Write a Python program to create a list of even numbers and another list of odd numbers from a given list.
  6. Write a Python program to remove repeated elements from a given list without using built-in methods.
  7. Write a Python program to find the longest word in a given sentence.
  8. Write a Python program to find the number of occurrences of a given number without using built-in methods.
  9. Given the list: ["www.zframez.com", "www.wikipedia.org", "www.asp.net", "www.abcd.in"], write a Python program to print the website suffixes ( com, org, net, in) from this list.
  10. Write a Python program to sort a given list of numbers without using the sort() function.
  11. Write a Python program to read a list of numbers and print the second largest number without using built-in functions.
  12. Write a Python program to merge two sorted lists into a single sorted list without using built-in methods.
  13. Write a Python program to rotate a list to the right by a given number of positions. For example, if the list is [1, 2, 3, 4, 5] and the number of positions is 2, the output should be [4, 5, 1, 2, 3].
  14. Write a Python program to find the difference between the maximum and minimum numbers in a list without using built-in functions.
  15. Write a Python program to flatten a list of lists into a single list. For example, given [[1, 2], [3, 4], [5]], the output should be [1, 2, 3, 4, 5].
  16. Write a Python program to find all pairs in a list that sum up to a given number. For example, given the list [1, 2, 3, 4, 5] and the target sum 6, the output should be [(1, 5), (2, 4)].
  17. Write a Python program to check if two lists are identical (contain the same elements in the same order).
  18. Write a Python program to remove the first occurrence of a given element from a list without using built-in methods.

We’d love to hear your feedback and suggestions about this article. Feel free to reach out to us using the WhatsApp number below.

Sajith Achipra has been a trainer and testing consultant at Zframez Technologies since 2009. With 15+ years of experience, he specializes in networking, Python, development, and testing. He conducts online courses to help students and professionals enhance their skills. You can reach him on WhatsApp at +91 8884 884 844 for your training and testing requirements.