简体   繁体   中英

List of Dictionary to list of tuples for loop in python

I am trying to create a conditional for loop for a list of dictionaries.

The List of Dictionaries is as follows:

CLIENTS_EXAMPLE = [
    {
        "first-name": "Elsa",
        "last-name": "Frost",
        "title": "Princess",
        "address": "33 Castle Street, London",
        "loyalty-program": "Gold",
    },
    {
        "first-name": "Anna",
        "last-name": "Frost",
        "title": "Princess",
        "address": "34 Castle Street, London",
        "loyalty-program": "Platinum",
    },
{
        "first-name": "Ben",
        "last-name": "Frost",
        "middle-name": "john",
        "title": "Prince",
        "address": "36a Castle Street, London",
        "loyalty-program": "Platinum",
    },

    {
        "first-name": "Harry",
        "middle-name": "Harold",
        "last-name": "Hare",
        "title": "Mr",
        "email-address": "harry.harold@hare.name",
        "loyalty-program": "Silver",
    },
    {
        "first-name": "Leonnie",
        "last-name": "Lion",
        "title": "Mrs",
        "loyalty-program": "Silver",
    },
]

I have to print any individuals with a known Address (not email address) and print out their first, middle (if available) and last name with Address.

The output should look like this:

[('Princess Elsa Frost', '33 Castle Street, London'),('Prince Ben John Frost', '36a Castle Street, London') ... etc]

I have managed to filter out the items and add them to a list of Tuples:


def process_clients(segment):

   new_list = []
   new_tuple =()
      for x in segment: #separates the dictionary from the list
         if "address" in x:
             if "middle-name" in x:
                new_tuple += (x["title"] + " " + x["first-name"] + " " + x["middle-name"] + " " + x["last-name"]), (x["address"])
             else:
                new_tuple += (x["title"] + " " + x["first-name"] + " "  + x["last-name"]), (x["address"])
                new_list.append(new_tuple)

       print(new_list)

process_clients(CLIENTS_EXAMPLE)

What I get is the following output:


[('Princess Elsa Frost', '33 Castle Street, London'), ('Princess Elsa Frost', '33 Castle Street, London', 'Princess Anna Frost', '34 Castle Street, London'), ('Princess Elsa Frost', '33 Castle Street, London', 'Princess Anna Frost', '34 Castle Street, London', 'Prince Ben john Frost', '36a Castle Street, London')]

How do I avoid the repetitions in the tuples?

Thanks

You're making a new tuple and then appending to it, you just need to move that new_tuple initialization inside the for loop

for x in segment:
    new_tuple =()

You could also use a list comprehension

[("{} {} {}{}".format(
    x["title"],
    x["first-name"],
    x["middle-name"] + " " if "middle-name" in x else "",
    x["last-name"])), x["address"]) for x in segment]

How do I avoid the repetitions in the tuples?

Don't use += ? When you write new_tuple +=... that's going to concatenate the existing new_tuple and whatever's on the right and set that as the new new_tuple . So you're starting with (), then you're adding Alfred so you get ('Alfred',) , append that to the list, then you add Brenda and get ('Alfred', 'Brenda') and add that to the list, etc...

You can just remove new_tuple entirely and append each tuple literal to your output list directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM