+ 2
Remove from a slice of a list
Why doesn't this code change the original list???? a=[2,1,2,4] a[1:].remove(2) print(a)----->a=[2,1,2,4]
27 Réponses
+ 6
In python, when you slice a list, you create a new list containing the sliced elements, and any modifications made to this new list do not affect the original list.
+ 4
Chris Coder ,
if we use a slice with a list, the result will be a list by default.
this means that we do not need to convert the result of a slice explicitely to list.
b = list(a[1:]) # >>> should be: b = a[1:]
+ 3
Lothar
I think OP is wondering why the output wasn't [2, 1, 4], given a[1:] is equal to [1, 2, 4].
From the help(list.remove), it says this function remove the argument from a list, but doesn't mention returning anything. In fact, after calling the remove method, it returns None.
b = a[1:].remove(2)
print(b) # None
So, a[1:].remove(2) alone produce nothing and not changing the original list.
+ 2
Mohammadamin ,
to remove the number 2 from the list, we do not need a slice. it can be done (inplace, without creating a new list) like:
a.remove(2)
this will remove the leftmost 2.
if we wanted to remove all numbers 2, there are several possible solutions.
+ 2
Lothar You are right.
It does not have to be explicitly done. However, My intention was to clarify that you can't simply modify the slice directly. Since the question is essentially “how to remove from a slice of a list”
Since slicing creates a new substring from the source string and the original string remains unchanged,
I used “list” as a clear illustration that I'm referring to a new, separate list.
+ 2
sarah shalom, Mohammadamin
Okay, that's fine. The original topic will get lost among all the new, unrelated stuff. And that'll make the whole post a confusing mess that's not helpful to anyone.
+ 2
List slicing returns a new list from the existing list. Thus the original list will remain unchanged.
+ 1
a[1:] creates a slice of the original list object a.
a[1:] Slice does not have a remove() method.
To print the slice
print(a[1:])
If you want to remove an element from a slice of a list, you can convert the slice to a list and then call the remove() method:
b =list( a[1:])
Then you may call the remove() method on list b
b.remove(2)
print(b)
Example:
a=[2,1,2,4]
a[1:]
print(a) # [2, 1, 2, 4]
print(a[1:]) # [1, 2, 4]
b = list(a[1:])
b.remove(2)
print(b) #[1, 4]
+ 1
sarah shalom create a new post so that you will recieve the attention you need for your question.
+ 1
/* (: teegame :)
basically its a bet game where user has to input
1 for playGame
0 for exit game
---- Input method-----
1
10
20
30
40
50
1
-----input done-----
Brief explanation :)
1 as a first input starts the game
then on a separate line you have to enter 5 unique numbers (numbers should not be same )
then you have to give bet number that will be challenge for the computerNumbers to match that specific bet or not like in the above input i bet computer 1 number
then output will be predicted according to situation
*/
import java.util.*;
public class teegame {
// Constants declared for the game parameters
private static int NUMBERS_TO_PICK = 5; // Number of numbers to pick
private static int MAX_NUMBER = 50; // Terminal value of specified range
private static int MAX_BET = 5; // Maximum bet
private static Scanner scanner = new Scanner(System.in);
private static ArrayList<Integer> playerNumbers = new ArrayList<>();
priva
+ 1
a[1:].remove(2) means you are removing 2 from the list a[1:]......not a. a is still same
0
Hello
0
I need help on how to form a game
0
Chris Coder It doesn't need to create a new post. It's ok.
0
Thanks when would you please send it
0
Says who?
0
All I need is a formular for making games on java
0
/* (: teegame :)
basically its a bet game where user has to input
1 for playGame
0 for exit game
---- Input method-----
1
10
20
30
40
50
1
-----input done-----
Brief explanation :)
1 as a first input starts the game
then on a separate line you have to enter 5 unique numbers (numbers should not be same )
then you have to give bet number that will be challenge for the computerNumbers to match that specific bet or not like in the above input i bet computer 1 number
then output will be predicted according to situation
*/
import java.util.*;
public class teegame {
// Constants declared for the game parameters
private static int NUMBERS_TO_PICK = 5; // Number of numbers to pick
private static int MAX_NUMBER = 50; // Terminal value of specified range
private static int MAX_BET = 5; // Maximum bet
private static Scanner scanner = new Scanner(System.in);
private static ArrayList<Integer> playerNumbers = new ArrayList<>();
priva
0
It might be very helpful