Zip function and messy data#
The zip () function creates objects that can be used to produce single items. This function can create pandas DataFrames by merging two lists.
Importing libraries and packages#
1# Mathematical operations and data manipulation
2from itertools import zip_longest
Generating a list of tuples#
1countries = ["India", "USA", "France", "UK"]
2capitals = ["Delhi", "Washington", "Paris", "London"]
3
4countries_and_capitals = [t for t in zip(countries, capitals)]
5countries_and_capitals
[('India', 'Delhi'),
('USA', 'Washington'),
('France', 'Paris'),
('UK', 'London')]
Generating a dict#
1countries_and_capitals_as_dict = dict(zip(countries, capitals))
2countries_and_capitals_as_dict
{'India': 'Delhi', 'USA': 'Washington', 'France': 'Paris', 'UK': 'London'}
Handling messy data#
Using the zip function to handle messy data in lists of unequal length.
1countries = ["India", "USA", "France", "UK", "Brazil", "Japan"]
2capitals = ["Delhi", "Washington", "Paris", "London"]
3
4# the ziplongest function from the itertools module can be used
5countries_and_capitals_as_dict_2 = dict(zip_longest(countries, capitals))
6countries_and_capitals_as_dict_2
{'India': 'Delhi',
'USA': 'Washington',
'France': 'Paris',
'UK': 'London',
'Brazil': None,
'Japan': None}