1

My task right now is to append two list together. Of course I know that I can do:

Names = ["Samson", "Daniel", "Jason", "Delion"]
Ages = [16, 18, 34, 27]
Names.extend(age)

However, what i am attempting to do is print these lists in the format of:

Samson   16
Daniel   18
Jason    34
Delion   27

With my current attempt, all ive been able to do is print in the format:

Samson, Daniel, Jason, Delion, 16, 18, 34, 27

Greatly appreciated if I could get some sort of help on how to do this.

  • `zip(Names, Ages)`? – Iain Shelvington Nov 29 '21 at 16:40
  • 1
    Does this answer your question? [Zip lists in Python](https://stackoverflow.com/questions/13704860/zip-lists-in-python) – MatBBastos Nov 29 '21 at 16:43
  • Also, I'd go against `\t` as the separator. Best to use `ljust` or just formatting the string with such as `f"{name:<10}{age}"`. See more [here](https://stackoverflow.com/questions/5676646/how-can-i-fill-out-a-python-string-with-spaces) – MatBBastos Nov 29 '21 at 16:45

4 Answers4

2

You can use a dictionary here.

Names = ["Samson", "Daniel", "Jason", "Delion"]
Ages = [16, 18, 34, 27]
a_dict = dict(zip(Names, Ages))
for key in a_dict.keys():
    print(key, a_dict[key])
havingaball
  • 378
  • 2
  • 11
1

You can use zip function.

names = ["Samson", "Daniel", "Jason", "Delion"]
ages = [16, 18, 34, 27]
new_list = list(zip(names, ages))
for name, age in new_list:
    print(f"{name}\t{age}")
Ratery
  • 2,863
  • 1
  • 5
  • 26
1
Names = ["Samson", "Daniel", "Jason", "Delion"]
Ages = [16, 18, 34, 27]

data = zip(Names, Ages)

for person in data:
    print(person[0], person[1])
Mikhail Sidorov
  • 1,325
  • 11
  • 15
0

Use the zip function

Names = ["Samson", "Daniel", "Jason", "Delion"]
Ages = [16, 18, 34, 27]
for name, age in zip(Names, Ages):
    print(f"{name}\t{age}")