AI Programming Machine Learning 920x470
AI Programming Machine Learning 920x470


Using lists in Python

A string is a variable that allows you to store multiple variables with an index. This is an extremely powerful programming tool that you will use again and again. This post will show you how lists work, how to make them, and how to add them to a list in Python!

What are lists?

A list is a collection of variables. Let’s use the example of a string. A string is a variable that stores a string of alphanumeric characters and symbols. This is used to store things like names and locations, and to display text to users on the screen.

Also read: Best Python Courses Online

But sometimes one string is not enough. For example, imagine taking a quiz with several questions. You want to be able to randomly and programmatically bring up these questions and add them to the list at any time.

One way to do this is to create hundreds of individual strings. We would then have to do some sort of massive, nested “IF / THEN” statement to sort the list. In the pseudocode:

IF randomQuestionNumber = 1 THEN PRINT “What is the capital of England?”

ELSE IF randomQuestionNumber = 2 THEN PRINT “Who is the President of the United States?”

ELSE IF randomQuestion = 3 THEN PRINT….

You get the picture!

That is not optimal.

Instead, we would add all of our strings to one long list. Think of it like a filing cabinet that holds our strings.

We do it in Python like this:

questions = ["What is the capital of England?", "Who is the president of the United States?", "What is the value of Pi to 5 digits?"]

As with so many other things, making lists in Python is extremely easy! All you need to do is enclose the items that make up your list in square brackets, separated by a comma.

Now that you know how to add a list in Python anytime you want to add more questions: just put an extra item in the square brackets!

Also read: What is Python and how do you get started?

What’s even better is that you don’t have to define the data type as Python can figure that out for us. We can even Mix Data types in our list:

questions = ["What is the capital of England?", 3, "Who is the president of the United States?", "What is the value of Pi to 5 digits?"]

How to add to a list in Python

If you want to know how to programmatically add to a list in Python, or how to append a list in Python, just use:

questions.append("How many continents are there?")

This will add an extra item to the end of the list.

But what if we want to know how to add a list in Python while placing the new value in a different position? For example, what if we want to add a new one? third Question?

To do this we would use:

questions.insert(2, "Who was the first man on the moon?")

The number is the “index” (that is, the point where we want to insert our value) and the string is the data we enter.

You may notice something strange here: to add a new one third Question, we are using the index 2. The reason for this seeming madness is that list indexes always start at 0. This applies to all programming.

So if you want to paste something at the Beginning from the list do this as follows:

questions.insert(0, "Who was the first man on the moon?")

Note that when you add a new item to your list, you also change the position of all subsequent items in this way.

If you don’t want to store data linearly, you can do it with another tool called a dictionary. But that’s a conversation for another time!

You can also use delete () or clear () to delete items from a list. Clearing clears the entire list, while clearing allows an index to be selected to remove a specific item.

How to get items from a list

Now what if we want to get one of these items?

It’s easy too! Just use the name of your list as you would with any other variable and put the index in square brackets after it. For example:

print(questions[2])

This prints the entry with the index “2” on the screen.

If we wanted to print the entire list, we could do it like this:

for x in range(0, len(questions)): print(questions[x])

This for loop steps through, incrementing the value of x from 0 to the length of the list.

Put all of the code together to see how to append a list in Python and do everything we just learned:

questions = ["What is the capital of England?", "Who is the president of the United States?", "What is the value of Pi to 5 digits?"]
questions.append("How many continents are there?")
print(len(questions))
questions.insert(2, "Who was the first man on the moon?")
print(questions[2])
for x in range(0, len(questions)): print(questions[x])

Now you know how to make a list in Python and append to it! If we were really going to create a quiz I would recommend saving your questions to a separate file and then pulling the list from there. That way, you don’t need to know how to add a list in Python as you can just update your text file. But that’s a story for another time!

Also read: How to call a function in Python

Would you like to develop your Python knowledge further? We recommend Coding with Python: training for budding developersThis is an absolute bargain as the course is valued at around $ 700.

$ 49 .99

Coding with Python: Training for Budding Developers Bundle

save up $ 641 .01

Buy It Now

Coding with Python: Training for Budding Developers Bundle Buy It Now

save up $ 641 .01 $ 49 .99


For more developer news, features, and tutorials from Android Authority, don’t sign up for the monthly newsletter below!

LEAVE A REPLY

Please enter your comment!
Please enter your name here