Title: How to print all words of a for loop in one line
Tags: python;python-3.x;formatting
Question: I have following set of data. 

```set = [('john', 'm', 23), ('maria', 'f', 17), ('john', 'm', 45), 
       ('stacy', 'f', 19), ('stacy', 'f', 21), ('mary', 'f', 32)]
```

I want my output to look like: 

```John: m(23) m(45)
Maria: f(17)
Stacy: f(19) f(21)
```

Right now my output looks like this with the following code.

Output:

```John:   m (23) 
Maria:   f (17) 
John:   m (45) 
Stacy:   f (19) 
Stacy:   f (21)
```

Code:

```for ind in set:
    i = ind[0]
    if i == "john":
        print("John:"," ", str(ind[1]), "({0}) ".format(str(ind[2])))
    elif  i == "maria":
        print("Maria:"," ", str(ind[1]), "({0}) ".format(str(ind[2])))
    elif i == "stacy":
        print("Stacy:", " ", str(ind[1]), "({0}) ".format(str(ind[2])))
```

Comment: Your `set` is a list of tuples. And you shouldn't give variables names of [built-in functions](https://docs.python.org/3/library/functions.html).


Here is another answer: You should first collect the data for each person, and then you can print for each person, one at a time like:

Code:

```data = [('john', 'm', 23), ('maria', 'f', 17), ('john', 'm', 45),
       ('stacy', 'f', 19), ('stacy', 'f', 21), ('mary', 'f', 32)]

# collect per person, except Mary.  We don't like Mary
as_dict = {}
for datum in data:
    as_dict.setdefault(datum[0], []).append(datum)

# print each person, except Mary.  We don't like Mary.
for name, items in as_dict.items():
    if name in set('john stacy maria'.split()):
        msg = '{}: '.format(name) + ' '.join('{} ({})'.format(i[1], i[2])
                                             for i in items)
        print(msg)
```

Results:

```john: m (23) m (45)
mary: f (32)
stacy: f (19) f (21)
maria: f (17)
```

Comment for this answer: I need to compare if the first index of my data matches with given name, ie, i = datum[0], if i == "john" then do this... as seen in the output i don't want mary to be printed
Comment for this answer: I have very large data. Among them I only I want John, Stacy, Maria to be printed.
Comment for this answer: Then simply exclude `mary`.  `if name != 'mary'`
Comment for this answer: Do you expect me to be chasing changing requirements?  I answered the question as presented.  I frankly saw your stacked if as a naive implementation, not a requirement.

Here is another answer: Here's a different approach in which you collect the names of all person from your input list, then you collect and combine all information associated with each person and finally you print these information:

```inSet = [('john', 'm', 23), ('maria', 'f', 17), ('john', 'm', 45), 
       ('stacy', 'f', 19), ('stacy', 'f', 21), ('mary', 'f', 32)]

# Get names of people
nameSet = sorted(set([info[0] for info in inSet]))

# Get all info associated with each of these people
displayInfo = [ " ".join(["".join([info[1], '(' + str(info[2]) + ')']) for info in inSet if name==info[0]]) for name in nameSet]

# Print these information, after capitalizing first letter of each name.
for info in zip(map(str.title,nameSet),displayInfo):
print(": ".join(info))
```

Output:

```John: m(23) m(45)
Maria: f(17)
Mary: f(32)
Stacy: f(19) f(21)
```


Here is another answer: Try this (it only prints for john, maria and stacy):

```data = [('john', 'm', 23), ('maria', 'f', 17), ('john', 'm', 45),
        ('stacy', 'f', 19), ('stacy', 'f', 21), ('mary', 'f', 32)]

permitted_names = ['john', 'maria', 'stacy']
output = {}
for name, gender, age in data:
    if name not in permitted_names:
        pass
    elif name not in output:
        output[name] = '{0}({1})'.format(gender, age)
    else:
        output[name] += ' {0}({1})'.format(gender, age)

for name, value in output.items():
    print("{0}: {1}".format(name, value))
```

Output:

```john: m(23) m(45)
maria: f(17)
stacy: f(19) f(21)
```


Here is another answer: You can use python's ```defaultdict``` for this:

```In [37]: from collections import defaultdict

In [38]: data = [('john', 'm', 23), ('maria', 'f', 17), ('john', 'm', 45),
    ...:        ('stacy', 'f', 19), ('stacy', 'f', 21), ('mary', 'f', 32)]

In [39]: d = defaultdict(list)
In [40]: for name, *rest in data:
    ...:     d[name].append(rest)

In [41]: for name, value in d.items():
    ...:     if name in ('john', 'maria', 'stacy'):
    ...:         strvalues = (' {}({})'.format(a, b) for a, b in value)
    ...:         print('{}: {}'.format(name, ''.join(strvalues)))
john:  m(23) m(45)
maria:  f(17)
stacy:  f(19) f(21)
```

Comment for this answer: I am not allowed to import any library.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 4, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2018:00:02/25/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><python-3.x><formatting>', 'Title': 'How to print all words of a for loop in one line', 'ViewCount': 68, 'id': 48977025, 'user_AboutMe': None, 'user_AccountId': 12732298.0, 'user_DisplayName': 'user9248483', 'user_Reputation': 53.0, 'user_Views': 5.0, 'user_id': 9248483.0}}