4
input_elements = ["a", "b", "c", "d"]
my_array = ["1", "2", "3", "4"]

the output I want is:

["1", "2", "3", "4", "a"]
["1", "2", "3", "4", "b"]
["1", "2", "3", "4", "c"]
["1", "2", "3", "4", "d"]

I tried:

for e in input_elements:
  my_array.append(e)

I know the code right above is wrong, so I am wondering how I can generate the output like that.

Bhargav Rao
  • 41,091
  • 27
  • 112
  • 129
Kristine
  • 77
  • 1
  • 7
  • These are lists, not arrays. Python also has an array data type in the `array` module, but it is used much less frequently than the built-in list type. – Sven Marnach Oct 05 '16 at 22:15

3 Answers3

5

You can use a list comprehension to solve your issue.

>>> input_elements = ["a", "b", "c", "d"]
>>> my_array = ["1", "2", "3", "4"]
>>> [my_array+[i] for i in input_elements]

The result looks like

>>> from pprint import pprint
>>> pprint([my_array+[i] for i in input_elements])
[['1', '2', '3', '4', 'a'],
 ['1', '2', '3', '4', 'b'],
 ['1', '2', '3', '4', 'c'],
 ['1', '2', '3', '4', 'd']]

See What does "list comprehension" mean? How does it work and how can I use it? for more details about them.

Community
  • 1
  • 1
Bhargav Rao
  • 41,091
  • 27
  • 112
  • 129
1

You need to make a copy of the old list in the loop:

input_elements = ["a", "b", "c", "d"]
my_array = ["1", "2", "3", "4"]
new_list = []
for e in input_elements:
  tmp_list = list(my_array)
  tmp_list.append(e)
  new_list.append(tmp_list)

print(new_list)

Output:

[['1', '2', '3', '4', 'a'], ['1', '2', '3', '4', 'b'], ['1', '2', '3', '4', 'c'], ['1', '2', '3', '4', 'd']]

Note that tmp_list = list(my_array) makes a new copy of my_array.

The above can be shortened by making tmp-list implicit:

input_elements = ["a", "b", "c", "d"]
my_array = ["1", "2", "3", "4"]
new_list = []
for e in input_elements:
  new_list.append(my_array + [e]) # the temporary copy is implicit
print(new_list)

The above can then be further shortened using lambdas (essentially making the loop implicit in this example):

input_elements = ["a", "b", "c", "d"]
my_array = ["1", "2", "3", "4"]
new_list = [my_array + [e] for e in input_elements]
print(new_list)
user1952500
  • 6,068
  • 3
  • 20
  • 34
  • @MikeMcMahon The temporary list is implicit in the list comprehension. You could use `new_list.appen(my_array + [e])` in the for-loop as well. Why should there be any difference in that regard? Everything that can be done with a list comprehension can be done with a for loop as well, and more. – Sven Marnach Oct 05 '16 at 22:11
  • One of the fundamentals in Python is to be pythonic. A list comprehension is both expressive and pythonic. – Mike McMahon Oct 05 '16 at 22:14
  • I agree about the pythonic part. But the OP who's asking the question seems to be new to python and maybe CS (based on the question alone). In such a case my opinion is that the pythonic way is not instructive. – user1952500 Oct 05 '16 at 22:16
  • I don't think the use of a temporary here is detrimental to understanding what is going on. Though that's debatable of course. To express the implied nature of what is happening under the hood while showing the way in which to code within the paradigm of a given language is probably the best way to do it. That said I'm neither the teacher nor the student! – Mike McMahon Oct 05 '16 at 22:27
1

I'm assuming the output you're getting is:

['1', '2', '3', '4', 'a', 'b', 'c', 'd']

...because, that's what I'm getting.

The problem is, in your loop, you're simply adding a new element to the existing array, then printing the "grand total." So, you add a, then you add b, then you add c, then d... all to the same array, then printing out the whole shebang.

The easiest solution for your particular problem is, in your for loop, print the array as it is, with the e selection concatenated. Like so:

input_elements = ["a", "b", "c", "d"]
my_array = ["1", "2", "3", "4"]

for e in input_elements:
  print my_array + [e]

That way, you're printing the array with the extra element, without actually affecting the original array... keeping it "clean" to loop back through and add the next element.

This method allows you to achieve the desired result without having to result to extra memory allocation or unnecessary variables.

If you have other things to do during the for loop, you could always add the element, then remove it after processing using the pop function, like so:

for e in input_elements:
  my_array.append(e)
  print my_array
  # Do some other nifty stuff
  my_array.pop()

Another option is to use List Comprehension, which allows you to iterate through an array as more of an inherent statement:

print [my_array+[e] for e in input_elements]

Christine
  • 694
  • 6
  • 19
  • 1
    `my_array.remove(e)` will perform a linear search in the list for the first element matching `e`, and remove that. That's wrong in some cases, and even in cases where it happens to work it is inefficient. `my_array.pop()` will do what you want in an efficient way. – Sven Marnach Oct 05 '16 at 22:13