DSLBG13
DSLBG13

Reputation: 23

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": "[email protected]",
        "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

Upvotes: 0

Views: 61

Answers (2)

Sayse
Sayse

Reputation: 43320

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]

Upvotes: 1

Masklinn
Masklinn

Reputation: 42492

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.

Upvotes: 0

Related Questions