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
a = [10, 20, 30, 40, 50, 60, 70, 80, 90]
print(len(a)) # 9
print(min(a)) # 10
print(max(a)) # 90
print(a + [100, 110, 120]) # [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]
print(a[0]) # 10
a[0] = 15
print(a) # [15, 20, 30, 40, 50, 60, 70, 80, 90]
del a[2]
print(a) # [15, 20, 40, 50, 60, 70, 80, 90]
Explanation:
len(a)
returns the number of elements in the list.min(a)
andmax(a)
return the smallest and largest elements in the list, respectively.a + [100, 110, 120]
concatenates another list toa
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
a = [0, 11, 22, 33, 44, 55, 66, 77, 88, 99]
print(a[0:7:1]) # [0, 11, 22, 33, 44, 55, 66]
print(a[0:7:2]) # [0, 22, 44, 66]
print(a[7:2:-1]) # [77, 66, 55, 44, 33]
Explanation:
- The syntax
a[x:y:z]
slices the list from indexx
toy
(exclusive), stepping byz
. - Positive
z
slices from left to right, while negativez
slices from right to left.
Lists Inside Lists
a = [10, 20, [30, 40, 50], 60]
print(a[2]) # [30, 40, 50]
print(a[2][0]) # 30
print(a[2][1]) # 40
Explanation:
- You can nest lists inside other lists and access them by indexing, allowing for multi-dimensional list structures.
List Methods in Python
a = [1, 2, 3, 4, 5, 2, 6, 7, 2, 8]
print(a.count(2)) # 3
a.append(4)
print(a) # [1, 2, 3, 4]
a.extend([7, 8, 9])
print(a) # [1, 2, 3, 4, [5, 6], 7, 8, 9]
a.pop()
print(a) # [1, 2, 3, 4, [5, 6], 7, 8]
a.remove(3)
print(a) # [1, 2, 4, [5, 6], 7, 8]
a.insert(2, 25)
print(a) # [1, 2, 25, 4, [5, 6], 7, 8]
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
a = [8, 2, 15, 4, 6, 20]
a.sort()
print(a) # [2, 4, 6, 8, 15, 20]
a.sort(reverse=True)
print(a) # [20, 15, 8, 6, 4, 2]
a.reverse()
print(a) # [2, 4, 6, 8, 15, 20]
Explanation:
sort()
sorts the list in ascending order by default, or descending order whenreverse=True
.reverse()
reverses the order of the elements in the list.
Tuple Operations in Python
a = (10, 20, 30, 40, 50)
print(len(a)) # 5
print(min(a)) # 10
print(max(a)) # 50
print(a.count(50)) # 1
print(a.index(50)) # 4
b = (60, 70)
c = a + b
print(c) # (10, 20, 30, 40, 50, 60, 70)
print(a * 2) # (10, 20, 30, 40, 50, 10, 20, 30, 40, 50)
Explanation:
- Tuples are similar to lists but immutable, meaning their elements cannot be modified once created.
count()
andindex()
work similarly to their list counterparts.- You can concatenate tuples and repeat them using the
+
and*
operators.
Programming Exercises :
- Write a Python program to find the sum of all numbers in a list.
- Write a Python program to find the largest number in a given list without using
max()
. - Write a Python program to find the common numbers between two lists.
- Write a Python program to print all even numbers from a given list.
- Write a Python program to create a list of even numbers and another list of odd numbers from a given list.
- Write a Python program to remove repeated elements from a given list without using built-in methods.
- Write a Python program to find the longest word in a given sentence.
- Write a Python program to find the number of occurrences of a given number without using built-in methods.
- 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. - Write a Python program to sort a given list of numbers without using the
sort()
function. - Write a Python program to read a list of numbers and print the second largest number without using built-in functions.
- Write a Python program to merge two sorted lists into a single sorted list without using built-in methods.
- 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].
- Write a Python program to find the difference between the maximum and minimum numbers in a list without using built-in functions.
- 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].
- 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)].
- Write a Python program to check if two lists are identical (contain the same elements in the same order).
- 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.
About The Author:
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.