Lists, sets, dictionaries
In the last chapter we saw the various data types present in Python, in this chapter we'll look at the high level data types in detail.
List
Watch on YouTube | Read the docs
An ordered collection of n values (n >= 0)
Creating a list is simple:
>>> l = [] # creates an empty list.
>>> l = list() # creates an empty list.
>>> l = [1, 2, 3]Each element of a list has an index, indices in Python start with 0.
Let's say we have a list of five values, a = [11,22,33,44,55].
Value
Positive Index
11
0
22
1
33
2
44
3
55
4
Indices can be negative.
Value
Negative Index
11
-5
22
-4
33
-3
44
-2
55
-1
List elements can be accessed using indices.
List elements can be modified.
List elements can be used just like any other variable.
List Methods.
append
The append function takes one argument and adds it to the end of the list.
extend
pop
pop deletes and returns one element. It takes one optional argument.
No argument is passed: Deletes and returns the last element.
Valid index is passed: Deletes and returns the value at that index.
copy
a.copy() creates a new list with the values of list a.
Other functions
Learning a language requires self practice! If we explain each and every function, we will hinder your path of exploring the language, we encourage you to use the help() function to find out more about other functions which are allowed on Lists.
Getting Help
We need to understand how to get help in Python.
help: returns the documentation of the data type.dir: returns all the methods valid for that data type.
Example:
Output of help
helpOutput of dir
dirSlicing
Slicing is an easy way to fetch sub parts of them. Lists, strings and tuples allow Slicing.
The syntax of slicing is l[start_index : end_index].
Slicing returns a new object (list or string or tuple) from the start_index to the end_index without including end_index.
Slicing also works with strings and tuples. We invite you to try them out. Don't just read this book, execute code!
Tuples
Read the docs | Watch the video
Tuples are read only lists. A tuple object, once created, doesn't allow us to add, delete or update an element. When we use the dir on a tuple object, we find that there are only two methods, count and index.
List vs Tuple
Lists are used when we are not sure how many values we'll be having.
Tuples are used when there is a fixed number of values to deal with.
Set
Watch on YouTube | Read the docs
Sets are same as lists with the following limitations:
Duplicate entries are not allowed
Sets can only have basic data types as elements (lists/dictionary/set/tuple)
Sets do not allow indexing
When we try to create a set from a list which has a list element, it is an error (only basic elements int, float, string are allowed) :
Sets allow various methods like add, copy, deepcopy, update, pop, remove.
Dictionary
Read the docs | Watch on YouTube
Dictionaries are key value pairs. Keys can only be hashable data types i.e. basic data types. There is no such restriction on the values.
Lists are indexed starting from 0, it is done by the interpreter itself. Dictionaries allow custom indexes i.e. keys.
Note: the representation of both set and dictionary is by using curly braces,
{}.
Dictionaries do not support slicing.
keys() and values() are two functions which return all the keys and values of the dictionary object. Since we can't use for i in syntax to loop over dictionaries, we have to do this
We also encourage you to try out everything we did in this chapter, again! (on strings too)
Exercises:
Find the number of occurances of 1 in the list [1,2,1,2,1,2,3,4,1,2,3].
Find all the unique elements of a list. Input; a = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4]. Output; [1, 2, 3, 4].
Solve the above problem using a dictionary.
Find the count of each element of a list except "\n". a = [1, 2, 2, 3] should return this output. 1 : 1,, 2 : 2, 3 : 1.
Create a random dictionary and print key : value pair in ascending order of keys. Input: a = {"IN":"India", "ES":"Español"}, output: "ES"":"Español", "IN":"India". You have to use
dirto find out the necessary functions.The same example as above, print in descending order.
Given two list, find their
common elements.
elements present in list a but not in b.
elements present in list b but not in a.
hint: use sets.
Print a reverse of a list without using the
reversemethod.
Links
Last updated
Was this helpful?