Danger Cat
Danger Cat

Reputation: 39

Turning certain characters of lists into strings

I'm working on my second assignment for school and am very new to programming. We are just covering lists in python and I'm having a little trouble. We're making a go fish game using lists, and I need to convert some of the characters into a list or a new string. Preferably a string since i need to concatenate it to another string.

We have a list of lists representing the cards for each player, looking something like pHands[0] = [ac,4c,2h,jd,ad], and i need to turn it into "a 4 2 j a"

def MyCards (pHands,player_number):

    card_list = []
    for i in range(len(pHands[0])):
        card_list = card_list + (pHands[0][i][0])
    return card_list

It is causing errors of not being able to concatenate str to list, and I'm not too familiar with the join function. Any help would be much appreciated!! Thank you so much!

Upvotes: 2

Views: 136

Answers (5)

rnbguy
rnbguy

Reputation: 1399

The error arose because you tried to concatenate list and a non-list.

There are many ways to do this.

Oneliner : def MyCards (pHands,player_number): return [i[0] for i in pHands[0]]

If you want to edit your own code :

def MyCards (pHands,player_number):
    card_list = []
    for i in range(len(pHands[0])):
        card_list.append(pHands[0][i][0]) 
        # remeber don't use concatenation here. i.e. card_list = card_list + [pHands[0][i][0]]
        # because it creates a new list and assign it to card_list and that is bad for space.
    return card_list

Upvotes: 0

karthikr
karthikr

Reputation: 99630

A better way would be to use list comprehensions

card_list = [item[0] for item in pHands[0]]

So, basically

def MyCards (pHands, player_number):
    return [item[0] for item in pHands[0]]

And of course, to fix your code as per cdhowie's answer.

Upvotes: 3

tommyvn
tommyvn

Reputation: 733

map takes an iterable as the second argument and the function to apply to each element as the first.

The string function .join() takes an iterable as an argument and uses the string it's called on to join up the elements.

And lastly, Card suits go up to 10, so all the solutions suggesting grabbing the 0 index will break on 10, incorrectly returning 1. Indexing from the first to second last character solves this, as in "10c"[0:-1] == "10".

" ".join( 
  map( lambda i: i[0:-1], ["ac","4c","2h","jd","ad"] )
)

Upvotes: 0

Pawel Miech
Pawel Miech

Reputation: 7822

Using list comprehensions:

>>> pHands = ['ac','4c','2h','jd','ad']
>>> " ".join([i[0] for i in pHands])
'a 4 2 j a'

join method of strings simply joins elements of iterable such as list into a string, list comprehension is an effective and fun way of itering over a list in Python.

Upvotes: 0

cdhowie
cdhowie

Reputation: 169018

Use card_list.append(pHands[0][i][0]).

+ only works in this context when the elements on both sides are lists. So you could also do:

card_list = card_list + [pHands[0][i][0]]

However, this would be wasteful as it (1) allocates a new temporary list object, and (2) returns a new list object instead of modifying the existing one.

Upvotes: 3

Related Questions