Title: Chain Together Lists
Slug: chain_together_lists
Summary: Chain Together Lists Using Python.
Date: 2017-02-02 12:00
Category: Python
Tags: Basics
Authors: Chris Albon

Preliminaries


In [1]:
from itertools import chain

Create Two Lists


In [2]:
# Create a list of allies
allies = ['Spain', 'Germany', 'Namibia', 'Austria']

# Create a list of enemies
enemies = ['Mexico', 'United Kingdom', 'France']

Iterate Over Both Lists As A Single Sequence


In [3]:
# For each country in allies and enemies
for country in chain(allies, enemies):
    # print the country
    print(country)


Spain
Germany
Namibia
Austria
Mexico
United Kingdom
France