• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Data Dependence

Data Dependence

Learn about software development. Covering topics such as Coding in Python and JavaScript, developer productivity and more.

  • Home
  • Top Resources
  • Who Are You?
  • Show Search
Hide Search
You are here: Home / Programming / How to Write Beautiful Code with Pythonic Idioms | Lists

How to Write Beautiful Code with Pythonic Idioms | Lists

· · Leave a Comment

If you are reading this post then you probably want to write beautiful, clean, readable code that has people’s mouths watering when they look at it. Well done, that’s the right mindset to have. If you work or are planning to work for a company, or maybe contribute to open source software there is something you should really keep in mind. Lots and in the case of open source, possibly thousands of other people have to read your code.

Depending on the current state of the code you write it could be a very large, or very small job. Whichever it is, it is definitely worth putting a little time and effort into. The best way to do it is step by step, little by little. Think of it like a loop and with each iteration, add a new change to the way you code. That’s what I’ve been doing and it’s working well. I can also look through my past code and visibly see my code becoming better and better.

Anyway, let’s get on with the second post in this series on common pythonic idioms and dive into lists.

HOW TO HANDLE LISTS, THE PYTHONIC WAY

Lists are a very commonly used data structure and have many uses. They are one of Python’s sequence types, which means they can be used with many of the built in Python methods that take them.

Again, when dealing with lists there are often more than one way of doing things, but there will often be a ‘pythonic’ way. These are usually faster and more readable than the others, so let’s make use of them.

CREATING A LIST INITIALISED WITH X VALUES

When creating a list you will often want to initialise it a with certain number of values.

#How not to create a list initialised with x values
new_list = []
for x in range(10):
  new_list[x] = None
  
#How to create a list initialised with x values
new_list = [None] * 10

As you can see here Python has a very useful and compact way of doing this. By simply adding an asterisk and the amount of times you want the value to repeat, you can easily create a list containing that value x times.

LIST COMPREHENSIONS

List comprehensions are actually a fairly large topic and an extremely versatile tool to have in your toolbox. I definitely recommend reading over the Python documents for these.

#An example of a list comprehension
list_1 = range(1, 101)
list_comp = [val for val in list_1 if val % 2 == 0]

This example takes the variable list_1 which contains all integers from 1 to 100 and creates a new list containing only the numbers in list_1 that are divisible by 2. It can be used like so: [f(x) for x in sequence (conditional)] where f(x) is some function of x and (conditional) is an optional conditional statement. They can also be nested within one another.

JOINING A LIST INTO A STRING

Joining a list into one string is another task that you may find yourself having to do. In Python this is very easy, but at least in my opinion not very intuitive.

#How not to join a list into a string
strings = ["H", "e", "l", "l", "o"]
string = ''
for char in strings:
  string = string + char
  
#How to join an list into a string
strings = ["H", "e", "l", "l", "o"]
string = ''.join(strings)

As shown in the above code, the pythonic way to join a list into a string is to call the join() function on an empty string, passing it the list that you want to combine. You can read more about this in the Python documentation.

Lists. You will use them a lot in your Python career, so it’s definitely worth committing these to memory. I found that when I started using these idioms, my code instantly became more readable and was much easier to work with. I hope you can get some value out of these just like I did.

This was the second in a series of posts on common pythonic idioms.

Don't forget to share and follow!

Remember, don't forget to share this post so that other people can see it too! Also, make sure you subscribe to this blog's mailing list and follow me on Twitter so that you don't miss out on any useful posts!

I read all comments, so if you have something to say, something to share or questions and the like, leave a comment below!

‘Ello, I’m Jamal – a Tokyo-based, indie-hacking, FinTech software developer with a dependence on data.

I write Shakespeare-grade code, nowadays mostly in Python and JavaScript and productivity runs in my veins.

I’m friendly, so feel free to say hello!

Twitter is the best place for a chat.

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Pocket (Opens in new window)

Related

Programming idioms, lists, python, pythonic

Primary Sidebar

LEVEL UP with the Data Dependence Newsletter! Subscribe!


Top Posts & Pages

  • How to Find Unclosed Tags and Brackets Using a Stack
  • How to Build a GUI in Python 3.5
  • Mocking in Python - How to Bypass Expensive and External Code Within Your Tests
  • How to Logically Group Your Python Code into Modules
  • A Quick Guide to Slicing in Python - Become a Python Ninja

Data Dependence

Copyright © 2023 · Monochrome Pro on Genesis Framework · WordPress · Log in

  • Home
  • Top Resources
  • Who Are You?