...
/Collections: Lists, Tuples, Sets, and Dictionaries
Collections: Lists, Tuples, Sets, and Dictionaries
Understand some of Python's collection data types such as lists, tuples, sets, and dictionaries.
We'll cover the following...
Lists
The next datatype is the list
. A list
can be a collection of anything; think of it as a one-dimensional array. It has one “row” but as many “columns” as you want. Usually, the data types are the same in a given list
, but that convention does not have to be followed. Lists
can be sliced just like strs
. The official datatype name is list
or []
, so do not name any list
variable plain old list
!
list_of_nums = [1, 2, 3, 4, 5]print('list_of_nums[0] -->', list_of_nums[0])print('len(list_of_nums) -->', len(list_of_nums))
You can add to lists
by using the append()
method, which accepts one argument: the variable you want to add to the list
.
list_of_nums = [1, 2, 3, 4, 5]list_of_nums.append(7)print(list_of_nums)
A list
is almost exactly like an array type in MATLAB. There are some small differences between the two data types, but you can ...