8

Suppose there are two lists:

['a', 'b', 'c'], ['d', 'e', 'f']

what I want is:

'ad','ae','af','bd','be','bf','cd','ce','cf'

How can I get this without recursion or list comprehension? I mean only use loops, using python?

sangheestyle
  • 1,037
  • 2
  • 16
  • 28
Linda dadad
  • 97
  • 1
  • 6
  • 2
    show us some code please? – Jude Niroshan Mar 03 '17 at 04:09
  • You shouldn't be imposing restrictions like "no comprehensions" or "no recursion". You're artificially restricting your range of possible answers before even seeing them, when some of them could be the correct one. – skrrgwasme Mar 03 '17 at 04:30
  • because I know how to do with list comprehensions, I just want some advanced ideas about loop! – Linda dadad Mar 03 '17 at 04:32
  • ...How could you understand it in a comprehension but not a loop? Comprehensions are just a concise syntax for loops. Unroll the comprehension and you have the loop. – skrrgwasme Mar 03 '17 at 04:35
  • I realize that comment probably sounds a bit condescending, but I don't mean for it to. I really don't understand. Loops are a basic feature of every language, and I've never known anyone who learns about comprehensions before loops. – skrrgwasme Mar 03 '17 at 04:36
  • ..uh..because I have read other questions similar to mine, they use list comprehensions (of course in a loop), I only want to know how to write a loop without comprehension cuz I have never seen before the comprehension they used. Guys, I want some clear thoughts although they may long to write down... – Linda dadad Mar 03 '17 at 04:39
  • I think working through a [Python tutorial](https://developers.google.com/edu/python/) to learn the language basics would be a better approach than just asking for examples of loops here. – skrrgwasme Mar 03 '17 at 04:42
  • @Lindadadad I think his answer is perfect given what you've asked for. I just don't think your question is a good fit for this site. Don't get me wrong, asking for examples is a reasonable question, just not on SO. The site is here to help solve problems you encounter when implementing a program, not necessarily for teaching language basics. – skrrgwasme Mar 03 '17 at 04:47
  • Thank you for your advice. *) – Linda dadad Mar 03 '17 at 04:51
  • @ Keerthana Prabhakaran thanks – Linda dadad Mar 03 '17 at 04:53

3 Answers3

10

The itertools module implements a lot of loop-like things:

combined = []

for pair in itertools.product(['a', 'b', 'c'], ['d', 'e', 'f']):
    combined.append(''.join(pair))
Blender
  • 289,723
  • 53
  • 439
  • 496
  • TL;DR; Best option always is to use built in modules or libraries from python. I measure the performance of the answer using itertools and using for loop nested: `for_loop: 9.61 seconds` `itertools. product: 4.323 seconds` – Víctor Quilón Mar 02 '22 at 16:04
5

While iterating through the elements in the first array, you should iterate all of the elements in the second array and push the combined result into the new list.

first_list = ['a', 'b', 'c']
second_list = ['d', 'e', 'f']
combined_list = []
for i in first_list:
    for j in second_list:
        combined_list.append(i + j)
print(combined_list)
Mike
  • 1,495
  • 15
  • 11
1

This concept is called a Cartesian product, and the stdlib itertools.product will build one for you - the only problem is it will give you tuples like ('a', 'd') instead of strings, but you can just pass them through join for the result you want:

from itertools import product
print(*map(''.join, product (['a','b,'c'],['d','e','f']))
lvc
  • 34,233
  • 10
  • 73
  • 98