banner



How To Create A List Of Certain Length In Python

You cannot assign items to a list at positions that do not exist. This is because lists are indexed from zero and new index positions increment by one each time an item is added to a list.

form-submission

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Get exclusive scholarships and prep courses

form-submission

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Get exclusive scholarships and prep courses

This means if you want to add items to a particular position in a list, there needs to be a placeholder value in that position.

In this guide, we discuss how to initialize a list of size n in Python. We walk through an example of how to do this so you can initialize lists with custom sizes in your programs.

Python: Initialize List of Size N Using Multiplication

We're going to build a leaderboard application that tracks the positions of the winners of a math tournament. This leaderboard will display the last eight students in the tournament.

Students are added in descending order. This is because the first student in the list is the winner and the eighth student is in the eighth place.

To start, create an empty list in Python:

leaderboard = [None] * 8 print(leaderboard)

This code creates a list object with eight values. Each of the values in the list will be equal to None.

Our code returns our list of None values:

81% of participants stated they felt more confident about their tech job prospects after attending a bootcamp. Get matched to a bootcamp today.

The average bootcamp grad spent less than six months in career transition, from starting a bootcamp to finding their first job.

[None, None, None, None, None, None, None, None]

The value None could be substituted with any default value that you want. For instance, you could make each default value an empty string (""), the number zero (0), or something else.

The quarter-final has just completed and two contestants have been eliminated. The following contestants can now be added to our leaderboard:

  • Chad (seventh place)
  • Eileen (eighth place)

To add these students to our list, we use the assignment notation:

leaderboard[7] = "Eileen" leaderboard[6] = "Chad"

We set the value of the item at position 7 to Eileen and the value at position 6 to Chad. This is because lists are indexed from zero. The first item in our list has the value 0 and the last item has the value 7. This means Eileen is last in our list and Chad is second from last.

Next, let's write a "for" loop that displays all the positions in our leaderboard and the people who currently hold that position:

for l in range(0, len(leaderboard)): 		 if leaderboard[l] != None: 		         value = leaderboard[l] 	     else: 		         value = "TBD"  	     print("{}: {}".format(l + 1, value))

We use a range() statement to create a list of values between 0 and the length of the "leaderboard" list. This lets us iterate over our list of students in the leaderboard while keeping track of each position.

In our loop, we check if the value at a particular position in the list is equal to None. If it is not, the value at that position is assigned to the variable "value". Otherwise, the value "TBD" is assigned to the variable "value".

form-submission

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Get exclusive scholarships and prep courses

Next, we print out the value of the "value" variable. We also print out the value of "l" plus one. This is because lists are indexed from zero and to show a rank we should add one to that value. Otherwise, our leaderboard would start from zero.

Let's run our code and see what happens:

[None, None, None, None, None, None, None, None] 1: TBD 2: TBD 3: TBD 4: TBD 5: TBD 6: TBD 7: Chad 8: Eileen

Our code first prints out a list of None values. Next, our code assigns values to the last two items in our list. Finally, our code prints out the positions in our leaderboard.

Python: Initialize List of Size N Using range()

We can initialize a list of size n using a range() statement. The difference between using a range() statement and the None method is that a range() statement will create a list of values between two numbers. By default, range() creates a list from 0 to a particular value.

Let's initialize our leaderboard list using a range() statement:

leaderboard = list(range(8)) print(leaderboard)

The range() statement creates a sequence of values between 0 and 7. We convert that sequence into a list so we can iterate over it. This is only necessary in Python 3.

Our code returns a new list with eight values:

Conclusion

You can initialize a list of a custom size using either multiplication or a range() statement.

The multiplication method is best if you want to initialize a list with the same values. A range() statement is a good choice if you want a list to contain values in a particular range.

Now you have the skills you need to initialize a Python list of size n like an expert!

How To Create A List Of Certain Length In Python

Source: https://careerkarma.com/blog/python-initialize-list-of-size-n/

Posted by: dumaisention.blogspot.com

0 Response to "How To Create A List Of Certain Length In Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel