-2

i'm fairly new to python Language . I have two list like this: and i don't install the numpy package yet .

First_List = [10, 2, 5, 4, 100]

Second_List = [5, 10, 20, 20, 25]

How can I Divide each element in First_List by elements in the Second_List? The output should be:

Result = [2, 0.2, 0.2, 4]

and i want a List in the output, and not an array ! how can i do this ? i try using this :

>>> First_List = [10, 2, 5, 4, 100]
>>> Second_List = [5, 10, 20, 20, 25]
>>> First_List/Second_List

but i got this error :

Traceback (most recent call last): File "", line 1, in First_List/Second_List TypeError: unsupported operand type(s) for /: 'list' and 'list'

Nima Geran
  • 115
  • 1
  • 3
  • 10
  • 3
    `[x/y for x,y in zip(First_List,Second_List)]`? – Willem Van Onsem Mar 27 '17 at 13:23
  • 5
    Show what you have tried already so we can point you in the right direction. If you show your code, the community will be able to actually help explain to you what mistakes you are making, which will be a much better learning lesson than just being given the answer. – idjaw Mar 27 '17 at 13:24
  • @NimaGeran Please do not put code in the comments. That is extremely unhelpful. Edit your question and put the code in there, and use the formatting tools so that it is readable and understandable for other readers. – idjaw Mar 27 '17 at 13:29
  • i try using numpy to do that but i don't get what i need . – Nima Geran Mar 27 '17 at 13:29
  • 1
    @NimaGeran Do you have to use numpy to solve this? Or did you decide to use numpy because you think you need it? Because this is definitely not something that *needs* numpy. – idjaw Mar 27 '17 at 13:30
  • "i don't get what i need" is not describing a problem – khelwood Mar 27 '17 at 13:30
  • But, numpy *does* make things very easy for this type of operation. Numpy's solution is certainly more readable than `[x/y for x,y in zip(a,b)]`... – blacksite Mar 27 '17 at 13:32
  • 1
    But, why install a whole package for the argument that it is more readable? That is ridiculously overkill. Furthermore, what you wrote is very readable, also, there is nothing wrong with "exploding" that to make it even more readable if that is *not* readable enough. – idjaw Mar 27 '17 at 13:34
  • @NimaGeran Based on the code you just showed, you really need to revise why list division like that does not work and you need to actually iterate through your lists and divide the *numbers* among each other. Please refer back to your course notes or whatever tutorial you are following to know how to divide numbers inside a list – idjaw Mar 27 '17 at 13:35
  • @idjaw i dont install numpy yet, is there a way to not install it ? – Nima Geran Mar 27 '17 at 13:36
  • @WillemVanOnsem `x/y` assumes Python 3 because of floor division, also with Python 3 it always `return`s a `float` (e.g. `10 / 5` is `2.0`) and they want an `int` here apparently – Chris_Rands Mar 27 '17 at 13:36
  • BTW, an entry is missing in `Result`: 4/20 = 0.2 – PM 2Ring Mar 27 '17 at 13:37
  • Thank you for your comments friends ! – Nima Geran Mar 27 '17 at 13:49

1 Answers1

6

Assuming both First_List and Second_List have same number of elements, you can iterate for the index range any of the list and divide element of one list from corresponding element of other list:

First_List = [10, 2, 5, 4, 100]
Second_List = [5, 10, 20, 20, 25]
result = []

# get last index for the lists for iteration
end_index = len(First_List)

for i in range(end_index):
    result.append(First_List[i]/Second_List[i])
result

Output:

[2.0, 0.2, 0.25, 0.2, 4.0]

This can be all done using list comprehension

result = [First_List[i]/ Second_List[i] for i in range(len(First_List))]

Alternatively, it can be done using zip and list comprehension (which is better). For details you can check here:

result = [a/b for a,b in zip(First_List,Second_List)]
Community
  • 1
  • 1
niraj
  • 17,498
  • 4
  • 33
  • 48