Data Structures
Data Structures
Data Structures
Python Programming
Using Problem Solving Approach
Reema Thareja
List is a versatile data type available in Python. It is a sequence in which elements are written as a list of
comma-separated values (items) between square brackets. The key feature of a list is that it can have elements
that belong to different data types. The syntax of defining a list can be given as,
List_variable = [val1, val2,...]
Examples
:
Similar to strings, lists can also be sliced and concatenated. To access values in lists, square brackets are used to slice
along with the index or indices to get value stored at that index. The syntax for the slice operation is given as, seq =
List[start:stop:step]
Example:
Example:
Example:
10
12
14
Example:
15
print(i)
16
17
18
The filter() function constructs a list from those elements of the list for which a function returns True. The
syntax of the filter() function is given as, filter(function, sequence)
As per the syntax, the filter() function returns a sequence that contains items from the sequence for which the function is
True. If sequence is a string, Unicode, or a tuple, then the result will be of the same type;
otherwise, it is always a list.
Example:
19
After applying the specified function on the sequence, the map() function returns the modified list. The map() function
calls function(item) for each item in the sequence and returns a list of the return values.
20
Example: Program to calculate the sum of values in a list using the reduce() function
21
Like lists, tuple is another data structure supported by Python. It is very similar to lists but differs in two things.
• First, a tuple is a sequence of immutable objects. This means that while you can change the value of one or more
items in a list, you cannot change the values in a tuple.
• Second, tuples use parentheses to define its elements whereas lists use square brackets.
Creating Tuple
Creating a tuple is very simple and almost similar to creating a list. For creating a tuple, generally you need to just
put the different comma-separated values within a parentheses as shown below.
Tup1 = (val 1, val 2,...)
where val (or values) can be an integer, a floating number, a character, or a string.
22
23
Example:
24
Examples:
25
26
Example:
27
28
Examples:
29
Examples:
30
Example:
31
The zip() is a built-in function that takes two or more sequences and "zips" them into a list of tuples. The tuple thus,
formed has one element from each sequence.
32
• Tuples are used to store values of different data types. Lists can however, store data of similar data types.
• Since tuples are immutable, iterating through tuples is faster than iterating over a list. This means that a
tuple performs better than a list.
• Tuples can be used as key for a dictionary but lists cannot be used as keys.
• Tuples are best suited for storing data that is write-protected.
• Tuples can be used in place of lists where the number of values is known and small.
• If you are passing a tuple as an argument to a function, then the potential for unexpected behavior due to
aliasing gets reduced.
• Multiple values from a function can be returned using a tuple.
• Tuples are used to format strings.
33
Sets is another data structure supported by Python. Basically, sets are same as lists but with a difference that sets are
lists with no duplicate entries. Technically, a set is a mutable and an unordered collection of items. This means that we
can easily add or remove items from it.
A set is created by placing all the elements inside curly brackets {}, separated by comma or by using the
built-in function set(). The syntax of creating a set can be given as,
34
35
36
37
38
39
Example:
40
41
42
43
44
Example:
45
46
47
48
Python also allows you to use string formatting feature with dictionaries. So you can use %s, %d, %f, etc. to
represent string, integer, floating point number, or any other data.
Example: Program that uses string formatting feature to print the key-value pairs stored in the dictionary
50
• Use lists to store a collection of data that does not need random access.
• Use lists if the data has to be modified frequently.
• Use a set if you want to ensure that every element in the data structure must be unique.
• Use tuples when you want that your data should not be altered.
51