-3

I want to declare an array in python3. I tried but I got an error

Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> ip_pb=[]
>>> ip_pb[0]="0111111100000000000000011110001"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> 

After,I did this,it is working

>>> ip_pb=[""]
>>> ip_pb[0]="0111111100000000000000011110001"
>>> print(ip_pb)
['0111111100000000000000011110001']

But,I am looking for an another method. If we don't know how many values in the array, we can't declare the array in above method.

5
  • 2
    Please read a tutorial on python lists. You seem to be lacking a lot of critical basic info about them Commented Jan 4, 2020 at 4:39
  • 2
    ip_pb=[], here list is empty, but you're trying to access 1st element which is out of the boundary/range of the list and hence the error. What's your question? Commented Jan 4, 2020 at 4:39
  • 2
    ip_pb.append() will insert an item at the end of your list. Commented Jan 4, 2020 at 4:39
  • 2
    Does this answer your question? IndexError: list assignment index out of range - Python with an array Commented Jan 4, 2020 at 4:42
  • Your way of initialization suggests that you might be coming from a php background. You might want to checkout: slideshare.net/bennuttall/python-for-php-developers Commented Jan 4, 2020 at 4:49

4 Answers 4

1

Based on your last line (that you don't know how many elements are there in the array), you want to create an empty array and then use the append() function to add values into it.

The modified version of the code which you have provided in the question:

ip_pb = []
ip_pb.append("0111111100000000000000011110001")
print(ip_pb)
Sign up to request clarification or add additional context in comments.

Comments

1

In Python, we usually call it list instead of the traditional array. list in Python is always a dynamically lengthened, that means it doesn't have a fixed size as opposed to what called array.

for fixed size array, I believe Python itself has a standard library which you can use it with import array but unfortunately it has limited data types. array

For the sake of simplicity, maybe you can just try this method:

def array(fill, n):
    return [fill] * n

a = array("", 10)
a[0] = "10010101"
a[9] = "10230123"
a[10] # IndexError

Comments

0

You should initialize your array if you want to use in this way:

>>> ip_pb = [None] * 5
>>> ip_pb
[None, None, None, None, None]
>>> ip_pb[1] = 3
>>> ip_pb
[None, 3, None, None, None]

Comments

0
  1. Create empty list(array) like x = []
  2. Add any number of elements like x.append(25), x.append(100)

You don't need to know the number of elements beforehand, but if you want to know the count of elements in your list then use

print(len(x))

To print complete list use

print(x)

To print each element separately

for i in x:
    print(i)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.