Find and index item inside a list of lists
Question:
This is for my python algorithms class:
So I have list database
which should have lists 1, 2 and 3 (item
) inside of it. I have to find a way to find items inside lists 1, 2 and 3 but also print the index of the list which that item belongs inside list database
This is how it’s setup:
database = []
name = input('type the name')
desc = input('type the description')
item = [name, desc]
database.append(item)
I’ve tried making a variable that scans for the specific item inside the lists of lists which kinda worked, but I can only make it show that this item exists in one of the item
lists and can’t actually index the list of which that item belongs to inside database
.
Answers:
This is one way to do it:
>>> database = [["pizza", "description-of-pizza"], ["burger", "description-of-burger"], ["soup", "description-of-soup"]]
...
>>> for index, (name, description) in enumerate(database):
... if name == "burger":
... print(index, name, description)
...
1 burger description-of-burger
You want to use the enumerate
function.
You give it an iterable (items through which you can iterate using a for loop), e. g. lists, strings, etc. and it gives you index, at which some item is stored within that iterable, and that very item.
For example, the code below:
for index, symbol in enumerate("Hi!"):
print(f"Index: {index}. Symbol: '{symbol}'.")
Will print the following:
Index: 0. Symbol: 'H'.
Index: 1. Symbol: 'i'.
Index: 2. Symbol: '!'.
enumerate
with other iterables will work the same.
The code that does your job:
database = []
for i in range(3):
name = input('Type the name: ')
desc = input('Type the description: ')
item = [name, desc]
database.append(item)
# database.append([name, desc]) - shorter
for index, item in enumerate(database):
# Do something with index and item
print(f'Index: {index}. Item: {item}')
This is for my python algorithms class:
So I have list database
which should have lists 1, 2 and 3 (item
) inside of it. I have to find a way to find items inside lists 1, 2 and 3 but also print the index of the list which that item belongs inside list database
This is how it’s setup:
database = []
name = input('type the name')
desc = input('type the description')
item = [name, desc]
database.append(item)
I’ve tried making a variable that scans for the specific item inside the lists of lists which kinda worked, but I can only make it show that this item exists in one of the item
lists and can’t actually index the list of which that item belongs to inside database
.
This is one way to do it:
>>> database = [["pizza", "description-of-pizza"], ["burger", "description-of-burger"], ["soup", "description-of-soup"]]
...
>>> for index, (name, description) in enumerate(database):
... if name == "burger":
... print(index, name, description)
...
1 burger description-of-burger
You want to use the enumerate
function.
You give it an iterable (items through which you can iterate using a for loop), e. g. lists, strings, etc. and it gives you index, at which some item is stored within that iterable, and that very item.
For example, the code below:
for index, symbol in enumerate("Hi!"):
print(f"Index: {index}. Symbol: '{symbol}'.")
Will print the following:
Index: 0. Symbol: 'H'.
Index: 1. Symbol: 'i'.
Index: 2. Symbol: '!'.
enumerate
with other iterables will work the same.
The code that does your job:
database = []
for i in range(3):
name = input('Type the name: ')
desc = input('Type the description: ')
item = [name, desc]
database.append(item)
# database.append([name, desc]) - shorter
for index, item in enumerate(database):
# Do something with index and item
print(f'Index: {index}. Item: {item}')