Skip to content
Fix Code Error

finding and replacing elements in a list

March 13, 2021 by Code Error
Posted By: Anonymous

I have to search through a list and replace all occurrences of one element with another. So far my attempts in code are getting me nowhere, what is the best way to do this?

For example, suppose my list has the following integers

>>> a = [1,2,3,4,5,1,2,3,4,5,1]

and I need to replace all occurrences of the number 1 with the value 10 so the output I need is

>>> a = [10, 2, 3, 4, 5, 10, 2, 3, 4, 5, 10]

Thus my goal is to replace all instances of the number 1 with the number 10.

Solution

>>> a= [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1]
>>> for n, i in enumerate(a):
...   if i == 1:
...      a[n] = 10
...
>>> a
[10, 2, 3, 4, 5, 10, 2, 3, 4, 5, 10]
Answered By: Anonymous

Related Articles

  • Form field border-radius is not working only on the…
  • How do I keep only the first map and when the game…
  • The definitive guide to form-based website authentication
  • What are the undocumented features and limitations…
  • Show/hide 'div' using JavaScript
  • How does PHP 'foreach' actually work?
  • What is your most productive shortcut with Vim?
  • Best way to replace multiple characters in a string?
  • Search code inside a Github project
  • Best way to communicate between instances of the…
  • Getting the closest string match
  • Set the local state using useEffect on Redux prop change
  • Using String Replace Only When Value Contains no…
  • How to find out client ID of component for ajax…
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Ukkonen's suffix tree algorithm in plain English
  • Is it possible to apply CSS to half of a character?
  • Why does C++ code for testing the Collatz conjecture…
  • Smart way to truncate long strings
  • How to print a string at a fixed width?
  • For-each over an array in JavaScript
  • Best way to sort through components Vue JS
  • Accessing query values in Next.js
  • Specifying java version in maven - differences…
  • Am I paranoid? "Brutally" big Polymer website after…
  • Iterator invalidation rules
  • Backbone model .toJSON() doesn't render all…
  • Accessing and filtering child components defined in…
  • Why doesn't the height of a container element…
  • How do I create a search box like on…
  • Construction and ordering of nested and template if=…
  • Implementing autocomplete
  • Backbone Relational not setting reverse relations properly?
  • powershell logic for no of times the value printed…
  • What does the CSS rule "clear: both" do?
  • Finding three elements in an array whose sum is…
  • Creating a SearchView that looks like the material…
  • Siemens LOGO! PLC data in the wrong order
  • Simplest way to create Unix-like continuous pipeline…
  • Database development mistakes made by application developers
  • Why do we use __init__ in Python classes?
  • Identifying and solving…
  • How can I add class active if the element click on…
  • Search input with an icon Bootstrap 4
  • What are the currently supported CSS selectors…
  • data.table vs dplyr: can one do something well the…
  • How can I wrap all BeautifulSoup existing…
  • How to filter a RecyclerView with a SearchView
  • What is difference between Lightsail and EC2?
  • Terraform: Call to function "element" failed: cannot…
  • How to use Servlets and Ajax?
  • How should a model be structured in MVC?
  • Python, Username and Password with 3 attempts
  • NextJS - can't identify where state change is…
  • Change column type in pandas
  • What are the nuances of scope prototypal /…
  • Include resource directory into Quarkus target output
  • React and Express iTunes Search API :: Error:…
  • How to handle Vue 2 memory usage for large data (~50…
  • What is the best way to signal 50+ nested components…
  • Shared behavior state across custom elements
  • Need multiple instances of Vuex module for multiple…
  • What is an IndexOutOfRangeException /…
  • Usage of __slots__?
  • What does if __name__ == "__main__": do?
  • Best practice multi language website
  • What's the difference between eval, exec, and compile?
  • Sorting 1 million 8-decimal-digit numbers with 1 MB of RAM
  • pandas.DataFrame.from_dict faster alternative
  • "Thinking in AngularJS" if I have a jQuery background?
  • What are Maven goals and phases and what is their…
  • Regex replacing single occurrences
  • How to render web component in Polymer
  • Replacing a 32-bit loop counter with 64-bit…
  • How to set HTML5 required attribute in Javascript?
  • What is a NullReferenceException, and how do I fix it?
  • Polymer 1.0 'array-style' path accessors,…
  • Find running median from a stream of integers
  • jQuery: Check if special characters exists in string
  • Webpack: Unable to find module with ID: main --…
  • Backbone.js - Best Practice for Implementing…
  • What does "Fatal error: Unexpectedly found nil while…
  • How should I choose an authentication library for…
  • Ember.js - creating dynamic filterProperty values…
  • Clear localStorage and change the view Backbone
  • Find CRLF in Notepad++
  • When to use LinkedList over ArrayList in Java?
  • How do I add a simple onClick event handler to a…
  • C# parse FHIR bundle - read resources
  • R: Turn timestamps into (as short as possible) integers
  • Search in vuejs with laravel backend api
  • Ember JS passing a parameter with insert-newline…
  • Can a website detect when you are using Selenium…
  • How do I use arrays in C++?
  • Add Keypair to existing EC2 instance
  • How to bind a Backbone.View to a 'single' DOM…
  • How do you change the current path name in real time…
  • commandButton/commandLink/ajax action/listener…
  • How to write integers alongside pixels in the…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How do I programmatically set the value of a select box element using JavaScript?

Next Post:

What’s the best UML diagramming tool?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error