Saving the position of a number removed from an array

how to sort an array and keep track of the index in java
find last element after deleting every second element in array of n integers
remove element from array javascript
remove odd numbers from an array java
javascript remove object from array
delete element from array c++
remove every nth element from array python
reorder array

I'm trying to create a card dealing program but am having slight difficulty. My code looks like this:

cards = ["ace of spades", "ace of hearts", "2 of diamonds"] etc...
firstcard = random.choice(cards)
del cards[firstcard]
print(firstcard)

I am trying to remove the card that is chosen from the array and store it (so I can delete it for it not to be dealt twice) at the same time in order to print it.

I have been receiving this error: TypeError: list indices must be integers or slices, not str, however I'm not sure what other functions exist to help me.

Thanks!

You could use random.randrange to randomly select item from list and list pop function to remove it from list:

import random

cards = ["ace of spades", "ace of hearts", "2 of diamonds"]

print(cards.pop(random.randrange(len(cards))))  # Randomly removed item.
print(cards)  # List after removal of the random item.

Position of an element after stable sort, Find the position of the last removed element from the array · Number of shuffles required for each element to return to its initial position · Sort the array according​  JavaScript Array elements can be removed from the end of an array by setting the length property to a value less than the current value. Any element whose index is greater than or equal to the new length will be removed.

You might consider using random.sample(cards, len(cards)) to generate a new list that is in shuffled order. This would avoid changing the original cards list. See https://docs.python.org/dev/library/random.html#random.sample

Python by Example: Learning to Program in 150 Challenges, If they enter a number between 10 and 20, save it in the array, otherwise display the message Remove it from the original array and save it in a new array. Once they have selected a number, display the position of that item in the array. The first parameter is the index : it specifies at what position of the array to add/remove items. Here we chose the index 2 (with the value “orange”). The second parameter is number of items to be removed. Here we set it to 0, so no items will be removed. The following optional parameters are the new item(s) to be added to the array.

So here I elaborated on your code. I sort of misread your question initially, but just added a simple fix by combining the final lists. This should return the cards to a new list in random order... if that is what you're after? In terms of dealing the cards out, you should be able to simply create a for loop and iterate through each card in the new list called shuffled_cards. Hope this helps, or at least part of it!

import random

cards = ["ace of spades", "ace of hearts", "2 of diamonds"] 
list_of_del_cards = []

i = 0

#while loop based on the number of cards in the list
while i < len(cards):

    selected_card = random.choice(cards) #randomly select card from list

    list_of_del_cards.append(selected_card) #append randomly selected card to a new list

    pos_in_list = cards.index(selected_card) #find pos of this card in old list

    del cards[pos_in_list] #del card from old list

    i += 1 #increment i each time to complete while loop

print(cards) #prints the list with the card that is left
print(list_of_del_cards) #print the list of deleted cards in order

shuffled_cards =  list_of_del_cards + cards

print(shuffled_cards)

Find last element after deleting every second element in array of n , The Naive approach is to remove every second element from the array until the size of Thus exactly n/2 numbers are removed, and we start as if from 1 in an array of N/2 O(log(n)) Auxiliary space: O(1). My Personal Notes arrow_drop_up​. Save on array whose each element is XOR of index value and previous element  Saving the position of a number removed from an array to remove the card that is chosen from the array and store it (so I can delete it for it not to be dealt

"What I’m trying to do is print each card as chosen in order, as if a dealer is placing them 1 by 1. Is there a way to move the string directly and put it in another list called dealt cards?" - OP

This task can be done with or without using a extra list, but if your desire is to store the values if say this sample is larger and you are storing 12 out of 52, then I provided that solution as well.

Without extra list:

import random
cards = ["ace of spades", "ace of hearts", "2 of diamonds"]

while len(cards) > 0:
    print(cards.pop(random.randrange(len(cards))))

With extra list 'dealt_cards':

import random
dealt_cards = []

while len(cards) > 0:
    x = cards.pop(random.randrange(len(cards)))
    dealt_cards.append(x)
    print(x)
print('Dealt Cards: {}'.format(dealt_cards))
2 of diamonds
ace of hearts
ace of spades
Dealt Cards: ['2 of diamonds', 'ace of hearts', 'ace of spades']

Readings in Artificial Intelligence and Software Engineering, in the LISP language: An index in an array may be represented as a LISP integer. property list marks, hash table entries, list removal, element overwriting​, etc. After choosing the state saving scheme, the body, initializer, incrementer and  Step 7 - Remove elements by filtering an array; Step 1: Remove Array Elements With the 'Splice' Function. Using 'Splice' is the most versatile way to remove an individual element from an array. It can be used to remove elements from any position in an array. Also, you can also use it to add elements back into the array.

9 Ways To Remove 🗑️ Elements From A JavaScript Array [Examples], So, how do you delete an element from a JavaScript array? Array index; filter - allows you to programatically remove elements from an Array It does not save the original array values, but removes matching elements. A negative number corresponds to the position in the array, counting from (but not including) the last element of the array. For example, -1 indicates the position just before the last element in the array.

remove(at:), The position of the element to remove. index must be a valid index of the array. Return Value. The element at the  It is unclear what is your function trying to achieve, without comments, but I would *guess* that SIZE is the actual size of the array and count is the number of useful elements in the array, in which case you're planning to swap the number out to the end of the array and reduce count, which is a legitimate approach.

Array, About Support Translate Sharing projects Offline support Save. Developers An array is a list of items that are numbers, booleans, or strings. Arrays an array. You find items in an array by knowing their positions. Arrays are flexible, they can grow and shrink in size. You can add and remove items at any place in the array. An _____ _____ describes the relationship between parallel arrays in which an element in the first array does not directly access its corresponding value in the second array binary search A ______ ______ is one that starts in the middle of a sorted list, and then determines whether it should continue higher or lower to find a target value

Comments
  • Notice that random.choice(cards) returns an element from the cards list (not an index). For example, it might return "ace of spades" instead of 1.
  • You could do cards.remove(firstcard), or use pop as suggested in @Austin's answer. See Difference between del, remove and pop on lists
  • What I’m trying to do is print each card as chosen in order, as if a dealer is placing them 1 by 1. Is there a way to move the string directly and put it in another list called dealt cards?
  • Are you looking to store randomly selected items to another list in the order in which they are removed from the current list?