bhylu.com

Advertisements

Join Lists

Combine lists with + when you want a new sequence, or extend an existing list when mutation is intended. The choice affects aliases.

Understand the idea

Both approaches keep duplicates and element order. A join of two lists is not a set union and does not merge records by a key. For many iterables, itertools.chain can avoid an intermediate combined list until you choose to materialise the result.

Where to try it

Open this example with Try in playground. The code loads as editable text and does not run until you select Run. The first run downloads Python. Each run starts fresh; temporary files created by a script are not files on your PC.

A worked example

Python

morning = ["Mira", "Dev"]
evening = ["Dev", "Noor"]
combined = morning + evening
print(combined)

Read the result

Dev appears twice because both source lists contain that name.

Try one change

Combine the lists while preserving first-seen unique names, then explain when that would be the wrong business rule.