Programming in Python (Udacity)

Farrukh Atabekov
3 min readOct 12, 2019

--

Python Data Structures

Lists in Python are equal to arrays in C++ in a sense that both those data types are sorted and indexed.

Lists

The difference between lists and strings is lists are mutable while strings are not. There two things to keep in mind about data types: Are they mutable? Are they ordered?

len(list) returns the number of the elements in the list. max(list) returns the max in the list(numbers list number), returns false if list is mixed. min(list). sorted(list). “/n”.join(list). list.append(‘december’)

Tuples

Tuples are immutable, ordered data types mainly used for storing data values that are closely related. Parentheses can be skipped when writing tuples. A tuple is an immutable, ordered data structure that can be indexed and sliced like a list. Tuples are defined by listing a sequence of elements separated by commas, optionally contained within parentheses: ().

Sets

Sets are data types that store lists of single values. Sets are unordered and you can pop elements from it. countries_set = set(countries) (sets single values). Their main use is collecting unique values. A set is a mutable data structure — you can modify the elements in a set with methods like add and pop. A set is an unordered data structure, so you can't index and slice elements like a list; there is no sequence of positions to index with!

Dictionaries

Unlike previous data types, dictionaries store two sets of data sets; key and value. They are unordered and we can access the values by their keys. Tuples can store two data sets too but dictionaries can have more values for a single key. A dictionary is a mutable, unordered data structure that contains mappings of keys to values. Because these keys are used to index values, they must be unique and immutable. For example, a string or tuple can be used as the key of a dictionary, but if you try to use a list as a key of a dictionary, you will get an error.

Built in method items() enables us to iterate through the list of items and print the keys and values of the dictionary appropriately.

for key, value in cast.items():
print("Actor: {} Role: {}".format(key, value))

It prints the key, values of the cast dictionary, making tuples of those values.

Zips and Enumerations

Zips and enumerations are ways to iterate through the list and make a new list or unzip an existing list. Zips returns a multiple lists into a tuple and it can also unzip an existing tuple and create two new lists of its items and values. For example,

list(zip(['a', 'b', 'c'], [1, 2, 3])) would output [('a', 1), ('b', 2), ('c', 3)].

In addition to zipping, we can also unzip the list using this method with an astericks

some_list = [('a', 1), ('b', 2), ('c', 3)]
letters, nums = zip(*some_list)

Enumerates especially come handy when you want to iterate through the list along with their indexes.

letters = ['a', 'b', 'c', 'd', 'e']
for i, letter in enumerate(letters):
print(i, letter)
Output
0 a
1 b
2 c
3 d
4 e

List Comprehensions

capitalized_cities = []
for city in cities:
capitalized_cities.append(city.title())

with lists they can easily be expressed as

capitalized_cities = [city.title() for city in cities]

--

--