jfox
jfox

Reputation: 898

How do I generate a list for each list item

names = ['apple', 'banana', 'orange']
prices1 = ['0.40', '1.20', '0.35']
prices2 = ['0.43', '1.21', '0.34']

How do I generate a list for each name and append a price(s) into that list

eg.

fruits = [['apple', ['0.40', '0.43']],
          ['banana', ['1.20', '1.21']], 
          ['orange', ['0.35', '0.34']]]

This is what I've been trying to use:

x = 0
n = len(names)
fruits = [[] for name in names]
for i in prices:
    for x in range(0, n-1):
        x += 1
        fruits[x].append(prices[x])

Edit

I want to be able to manipulate – add/remove prices from - the generated lists like

print[apple]

['0.40', '0.43']

or apple.append(prices3[x])

['0.40', '0.43', 'x']

Thanks so much for helping, I'm still learning

Upvotes: 2

Views: 1173

Answers (2)

zayora
zayora

Reputation: 418

Edit - using dictionaries:

Now that you have specified how you would like to manipulate your data, I'd strongly recommend to switch to using a dictionary instead of lists. Because of how the association of keys and values work, dictionaries will allow you to access a specific item by a more descriptive value than a numeric index, like a list does. Your new code would look something like this:

>>> names = ['apple', 'banana', 'orange']
>>> prices1 = ['0.40', '1.20', '0.35']
>>> prices2 = ['0.43', '1.21', '0.34']
>>> 
>>> fruits = {}     # fruits is now a dictionary, which is indicated by the curly braces
>>> for i in range(len(names)):
...     fruits[ names[i] ] = [ prices1[i], prices2[i] ]
... 
>>> print(fruits)
{'orange': ['0.35', '0.34'], 'apple': ['0.40', '0.43'], 'banana': ['1.20', '1.21']}

And if you ever need to check up on the prices of a specific fruit you could always use:

>>> print( fruits['apple'] )
['0.40', '0.43']

likewise, in order to add a new price you only need to type:

>>> fruits['banana'].append('1.80')
>>> print( fruits['banana'] )
['1.20', '1.21', '1.80']

And to remove a price:

>>> fruits['orange'].remove('0.34')
>>> print( fruits['orange'] )
['0.35']

To insert an entirely new item to the dictionary, simply use the = operator to attribute to the new key:

>>> fruits['durian'] = ['2.25', '2.33']
>>> print( fruits )
{'orange': ['0.35'], 'durian': ['2.25', '2.33'], 'apple': ['0.40', '0.43'], 'banana': ['1.20', '1.21', '1.80']}

And to remove an item, simply call the pop method:

>>> fruits.pop('apple')
['0.40', '0.43']
>>> print( fruits )
{'orange': ['0.35'], 'durian': ['2.25', '2.33'], 'banana': ['1.20', '1.21', '1.80']}

This way you will have a much clearer on what you're manipulating at any given time than by trying to juggle around obscure list indices.

If you must use lists, however, please refer to my old answer below.


Old answer:

Assuming the two lists of prices used were supposed to be assigned to two different variables, a solution would be to iterate over the lists like so:

>>> names = ['apple', 'banana', 'orange']
>>> prices1 = ['0.40', '1.20', '0.35']
>>> prices2 = ['0.43', '1.21', '0.34']
>>>
>>> fruits = []
>>> for i in range(len(names)):
...     fruits.append( [ names[i], [prices1[i], prices2[i]] ] )
...
>>> fruits
[['apple', ['0.40', '0.43']], ['banana', ['1.20', '1.21']], ['orange', ['0.35', '0.34']]]

Upvotes: 1

shx2
shx2

Reputation: 64308

You can use zip twice:

names = ['apple', 'banana', 'orange']
prices1 = ['0.40', '1.20', '0.35']
prices2 = ['0.43', '1.21', '0.34']
fruits = list(zip(names, zip(prices1, prices2)))

In python3, zip is a generator, thus we use fruits = list(...) to turn the generator into a list.

Upvotes: 2

Related Questions