How do I seperate parts of a list into multiple lists, and then put them all together into one big nested list?
Question:
I have a list that holds the names and ranks of five different cards (e.g 4 of spades, 2 of Hearts, etc..)
I need to be able to collect the first and third words of each ‘section’ in order to use it further. I had an idea to use nested lists which would keep each name and rank of a card in a list, and all 5 lists in a list of itself.
It’d look something like this:
[['King of Hearts'], ['4 of Clubs'], ['8 of Clubs'], ['Queen of Clubs'], ['9 of Diamonds']]
this way, using for
loops, I can call the first word of each list (the rank) and do it for all 5 lists in 2 lines of code.
However, whenever I try to append each individual name to a list, it just ends up as one big list with each separated.
I’ve tried using for loops that take the original list, and append each of those individually as a list. however, i don’t really know what i’m doing.
`
temp_card_list = list(p1cards)
card_list1 = []
for i in range(5):
print(temp_card_list[i])
card_list1.append(temp_card_list[i])
`
Answers:
I don’t know exactly what you want as an output, but based on your question here is how you would access the first and third element of every string, without a nested list:
cards = ['King of Hearts', '4 of Clubs', '8 of Clubs', 'Queen of Clubs', '9 of Diamonds']
for card in cards:
string_list = card.split(' ')
rank = string_list[0] # 1st word
suit = string_list[2] # 3rd word
print(f'rank: {rank} | suit:{suit}')
print('----------')
Output:
rank: King | suit:Hearts
----------
rank: 4 | suit:Clubs
----------
rank: 8 | suit:Clubs
----------
rank: Queen | suit:Clubs
----------
rank: 9 | suit:Diamonds
----------
I have a list that holds the names and ranks of five different cards (e.g 4 of spades, 2 of Hearts, etc..)
I need to be able to collect the first and third words of each ‘section’ in order to use it further. I had an idea to use nested lists which would keep each name and rank of a card in a list, and all 5 lists in a list of itself.
It’d look something like this:
[['King of Hearts'], ['4 of Clubs'], ['8 of Clubs'], ['Queen of Clubs'], ['9 of Diamonds']]
this way, using for
loops, I can call the first word of each list (the rank) and do it for all 5 lists in 2 lines of code.
However, whenever I try to append each individual name to a list, it just ends up as one big list with each separated.
I’ve tried using for loops that take the original list, and append each of those individually as a list. however, i don’t really know what i’m doing.
`
temp_card_list = list(p1cards)
card_list1 = []
for i in range(5):
print(temp_card_list[i])
card_list1.append(temp_card_list[i])
`
I don’t know exactly what you want as an output, but based on your question here is how you would access the first and third element of every string, without a nested list:
cards = ['King of Hearts', '4 of Clubs', '8 of Clubs', 'Queen of Clubs', '9 of Diamonds']
for card in cards:
string_list = card.split(' ')
rank = string_list[0] # 1st word
suit = string_list[2] # 3rd word
print(f'rank: {rank} | suit:{suit}')
print('----------')
Output:
rank: King | suit:Hearts
----------
rank: 4 | suit:Clubs
----------
rank: 8 | suit:Clubs
----------
rank: Queen | suit:Clubs
----------
rank: 9 | suit:Diamonds
----------