Breadcrumbs

Hints & Tips on Learning Python (List).

Python Logo

Here are some Python hints/tips that i have come across when learning to program Python and i thought to share them with you.

These small little Python code snippets that will enhance your bigger Python applications as you grow as a Python programmer.

Python Code example 1)...

Printing more than one list’s items simultaneoulsy

listcombine.py

# zip() function takes two equal length lists and mergers them together in pairs

list1 = [1,3,5,7]

list2 = [2,4,5,6]

for a, b in zip(list1,list2):

   print (a, b)

Python 3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27)

Type "help", "copyright", "credits" or "license()" for more information.

>>>$ python listcombine.py

1 2

3 4

5 5

7 6

>>>

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Python Code example 2)...

Partition a list into number of groups

listgroups.py

mylist = [‘Moon’, ‘Trees’, ‘Monkeys’, ‘Humans’, ‘Black’, ‘White’]

groups = list(zip(*[iter(mylist)]*2))

print (groups)

Python 3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27)

Type "help", "copyright", "credits" or "license()" for more information.

>>>$ python listgroups.py

[('Moon', 'Trees'), ('Monkeys', 'Humans'), ('Black', 'White')]

>>>

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Python Code example 3)...