Python Basics: Lists, Tuples, and Sets
One of the many reasons behind Python's popularity is its in-built data structures, which help developers store and organize data efficiently. Below we are exploring three fundamental data structures in Python:
- lists - mutable ordered collection
- tuples - immutable ordered collection
- sets - mutable unordered collection of unique elements
Lists
A list is a mutable, ordered collection of elements in Python. It can store elements of different data types, including integers, strings, and even other lists.
Creating a List
To create a list, you can use square brackets []
and separate the elements with commas. For example:
fruits = ['apple', 'banana', 'cherry']
numbers = [1, 2, 3, 4, 5]
List Methods and Operations
Accessing elements: You can access elements in a list using their index, starting with 0 for the first element:
fruits = ['apple', 'banana', 'cherry']
print(fruits[1]) # Output: banana
Adding elements: To add elements to a list, you can use the append()
method:
fruits = ['apple', 'banana', 'cherry']
fruits.append('orange')
print(fruits) # Output: ['apple', 'banana', 'cherry', 'orange']
Removing elements: To remove elements from a list, use the remove()
method:
fruits = ['apple', 'banana', 'cherry']
fruits.remove('banana')
print(fruits) # Output: ['apple', 'cherry']
Slicing: You can extract a portion of a list by specifying the start and end indices:
numbers = [1, 2, 3, 4, 5]
sliced_numbers = numbers[1:4]
print(sliced_numbers) # Output: [2, 3, 4]
Tuples
A tuple is an immutable, ordered collection of elements in Python. Once a tuple is created, you cannot modify its contents.
Creating a Tuple
To create a tuple, use parentheses ()
and separate the elements with commas:
fruits = ('apple', 'banana', 'cherry')
coordinates = (3, 4, 5)
Tuple Methods and Operations
Accessing elements: Accessing elements in a tuple is the same as in a list:
fruits = ('apple', 'banana', 'cherry')
print(fruits[1]) # Output: banana
Counting elements: To count the occurrences of an element in a tuple, use the count()
method:
numbers = (1, 2, 3, 2, 4, 2)
count_of_twos = numbers.count(2)
print(count_of_twos) # Output: 3
Finding the index: To find the index of the first occurrence of an element, use the index()
method:
fruits = ('apple', 'banana', 'cherry')
index_of_banana = fruits.index('banana')
print(index_of_banana) # Output: 1
Sets
A set is an unordered, mutable collection of unique elements in Python. Sets do not allow duplicate elements.
Creating a Set
To create a set, use curly braces {}
and separate the elements with commas, or use the set()
constructor:
fruits = {'apple', 'banana', 'cherry'}
numbers = set([1, 2, 3, 4, 5])
Set Methods and Operations
Adding elements: To add elements to a set, use the add()
method:
fruits = {'apple', 'banana', 'cherry'}
fruits.add('orange')
print(fruits) # Output: {'apple', 'banana', 'cherry', 'orange'}
Removing elements: To remove elements from a set, use the remove()
method:
fruits = {'apple', 'banana', 'cherry'}
fruits.remove('banana')
print(fruits) # Output: {'apple', 'cherry'}
Union: To combine two sets, use the union()
method or the |
operator:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
# or
union_set = set1 | set2
print(union_set) # Output: {1, 2, 3, 4, 5}
Intersection: To find the common elements between two sets, use the intersection()
method or the &
operator:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2)
# or
intersection_set = set1 & set2
print(intersection_set) # Output: {3}
Difference: To find the elements present in one set but not in another, use the difference()
method or the -
operator:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference_set = set1.difference(set2)
# or
difference_set = set1 - set2
print(difference_set) # Output: {1, 2}