Jshee
Jshee

Reputation: 2686

Append value into list and make inner list

So, I have a list like

['sm', 'Bug Out Bag']

How can I make list like

[['sm', 'The label I entered'], ['Bug Out Bag', 'Another label I entered']]

where labels are entered via input function?

I tried this code:

if action_choice_perform_bag in str(bag_actions[2]):
    for b in my_bag:
        apply_label_to_front = input('What label would you like to apply to front of bag? e.g. 30.0\n')
        my_bag[b].append(apply_label_to_front)
        # print(my_bag)   contains::: ['sm', 'Bug Out Bag']

but it complains that in indice in my_bag[b] isn't an integer, which it isn't.

Upvotes: 0

Views: 74

Answers (2)

Mike Müller
Mike Müller

Reputation: 85582

Just a few changes:

for item in my_bag:
    apply_label_to_front = input('What label would you like to apply to front of bag? e.g. 30.0\n')
    item.append(apply_label_to_front)

my_bag as dictionary with compression :

bag_names = ['sm', 'Bug Out Bag']
prompt = 'What label would you like to apply to front of bag? e.g. 30.0\n'
my_bag = {name: input(prompt) for name in bag_names}

Upvotes: 0

khelwood
khelwood

Reputation: 59212

You could do something like this:

list_of_lists = [ [b, input('What label?')] for b in my_bag ]

That is, use a list comprehension to make a new list, which contains lists that comprise an element from the original list, and a string from the user.

Upvotes: 1

Related Questions