How to add two lists with the same amount of indexs in python

2024/11/15 15:54:04

I am still new to coding so i apologize for the basic question. How do I add to elements of two seperate lists?

listOne = [0, 1 , 7, 8]
listTwo = [3, 4, 5, 6]
listThree = []for i in listOne:listAdd = (listOne[i] + listTwo[i])listThree.append(listAdd)
print(listThree)

I want the output to be [3, 5, 12, 14] but i keep getting a syntax error saying

TypeError: List indices must be integers, not strings. 

I thought this may have to do with i possibly being a string so i put int(i) as the first line after the for loop but this did not help.

Answer

You can use the zip() function. Change the + to whatever operation you want.

listOne = [0, 1 , 7, 8]
listTwo = [3, 4, 5, 6]l = [x + y for x, y in zip(listOne, listTwo)]print(l)[3, 5, 12, 14]
https://en.xdnf.cn/q/119302.html

Related Q&A

How to crawl thousands of pages using scrapy?

Im looking at crawling thousands of pages and need a solution. Every site has its own html code - they are all unique sites. No clean datafeed or API is available. Im hoping to load the captured data i…

Object Transmission in Python using Pickle [duplicate]

This question already has answers here:Send and receive objects through sockets in Python(3 answers)Closed last year.I have the following class, a Point objectclass Point:def __init__(self):passdef __i…

Google App Engine: Modifying 1000 entities

I have about 1000 user account entities like this:class UserAccount(ndb.Model):email = ndb.StringProperty()Some of these email values contain uppercase letters like [email protected]. I want to select …

more efficient method of dealing with large numbers in Python? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

MLM downline distribution count

I make my first MLM software and I think I managed to code how to get the points from the downline even though it is a recursive problem I didnt use recursion and I might refactor to a recursive versio…

Can someone please explain to me the purpose of the asterisk in Python? [duplicate]

This question already has answers here:What does asterisk * mean in Python? [duplicate](5 answers)How are pythons unpacking operators * and ** used?(1 answer)Closed 5 years ago.For instance, can some…

Linear Programming with cvxpy

I would like to ask you regarding on the Linear Program for optimization.I have an objective function, and constraint functions as below,variables(x1, x2, x3, x4, x5, x6) are quantities of the products…

Program runs forever without giving an error when plotting data only on continent [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

KeyError while perfoming solve of two equation

1st i need to get two equation of two longest line length i put lenghths with eq in list like these [( length 1 , eq 1 ) ,.....] sort list with reverse get two equation of two longest line when run the…

Most Pythonic way to merge two dictionnaries having common key/value pair

I have two lists of python dictionnaries : l1 = [{"id":1, "name":"A"}, {"id":2, "name":"B"}] l2 = [{"id":1, "full_name":&…