dimanche 22 mars 2020

For loop with dictionary and If-Condition

How can i get a new list of items with a for loop and an if-statement while working with dictionarys?

I have a list with a set of numbers and want to check if these numbers are in my dictionary that i created. The next step is then to create a new list, for every number that is inside the dictionary, with the values from it.

import pandas as pd

## Create new list with data inside .csv

df = pd.read_csv("data.csv", sep=";")

item_nr = df["Items"].tolist()

item_date = df["Date"].tolist()

# Creating a dict with for loop ("Item":"Date")

item_dict = {}

for i in range(len(item_nr)):
    item_dict[item_nr[i]] = item_date[i]

## Creating a list with dates from dictionary

date_list = []

for item in items_list_sold:
    if item == item_dict.keys():
        date_list.append(item_dict)

print(date_list)

Aucun commentaire:

Enregistrer un commentaire