How to Combine and Flatten Lists in Python with the For / In Loop
In this guide, we're going to extend our knowledge of the for-in loop to see how it can be used to merge multiple lists.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
  • Complete the Exercise
Video locked
This video is viewable to users with a Bottega Bootcamp license

Imagine that we have a scenario where we have a set of legacy customers and then we have new customers. So these could be from a different database or from just a file that contains a list of usernames anything like that. And so we have two lists and we need to combine them.

So we want to add Alice and Bob to the list that contains Tiffany and Kristine.

legacy_customers = ['Alice', 'Bob']
new_customers = ['Tiffany', 'Kristine']

Well, you may think that we can do something like this where we could just say that the raw database. So just raw_db equals and then combine legacy customers and then new customers. Let's see what this gives us if I say print raw_db this is going to print out Alice and Bob inside of their own list and then Tiffany and Kristine inside of their list.

large

Essentially all we've done is we've created a list that contains multiple lists which is not what we want. So I'm going to copy this just to walk through what we're actually going for. I'll add this is a comment.

So right now this is what we get but what we actually want is all of the strings to just be separate elements so we want to have four string elements instead of two list elements. So this is what we're actually looking to do and we can leverage the for-in loop in order to accomplish this.

So what I can do is I can say for legacy_customer so this is just our iterator variable here. I'm gonna say for legacy_customer in legacy_customers. I can then call our new customers list, so this is calling say imagine it's a new database or something like that. So I'm saying new customers and then I can just call the append function. So this is a function available inside of the list data type so I'm gonna say append and then legacy_customer and that is all I have to do.

for legacy_customer in legacy_customers:
  new_customers.append(legacy_customer)

So now I can say print and have our new customers so we don't even need to create a new list. We can simply tack on our legacy customers into this new customers list. And if I run this you can see we get the desired output.

large

So right here instead of having a list that contains two nested lists instead, we have simply added on to our new_customers database here and we've tacked on Alice and Bob. So the way that this is able to work is because we've utilized the for-in loop. So all we're doing is we're iterating through our legacy customer lists so we're just iterating through Alice and Bob and then with each loop we are saying I'm calling this new customers list here.

And then I simply want you to tack on the values. I want you to append the legacy customer as a new element on new customers. And so this may seem kind of trivial when we only have 2 items but imagine that you have 200.

Those are 200 items you would not want to have to code from scratch. In a real-life application, you wouldn't even know how many items you are going to be adding on so you couldn't hard code it in. But with a for-in loop, you can simply iterate over one list and then add each one of those elements into the new one.

The reason why this is able to work when what we have here online for didn't is this simply combined both of the lists and it treated them like two elements but with a for-in loop what we're doing is we are iterating over a preexisting list and we're not even treating these elements as anything except normal strings.

So this would be the same as if we said I want you to add one string element on to new_customers. Then the next time around I want you to add this other one and so on and so forth. So that is how you can create and combine multiple lists together using the for-in loop in Python.

Code

legacy_customers = ['Alice', 'Bob']
new_customers = ['Tiffany', 'Kristine']

raw_db = [legacy_customers, new_customers]

print(raw_db)

for legacy_customer in legacy_customers:
  new_customers.append(legacy_customer)

print(new_customers)