-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.

sshashank124
  • 31,495
  • 9
  • 67
  • 76
Janith
  • 403
  • 6
  • 14
  • 2
    Please read a tutorial on python `list`s. You seem to be lacking a lot of critical basic info about them – sshashank124 Jan 04 '20 at 04: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? – Ajay Dabas Jan 04 '20 at 04:39
  • 2
    ip_pb.append() will insert an item at the end of your list. – Stidgeon Jan 04 '20 at 04:39
  • 2
    Does this answer your question? [IndexError: list assignment index out of range - Python with an array](https://stackoverflow.com/questions/27267344/indexerror-list-assignment-index-out-of-range-python-with-an-array) – Stidgeon Jan 04 '20 at 04:42
  • Your way of initialization suggests that you might be coming from a php background. You might want to checkout: https://www.slideshare.net/bennuttall/python-for-php-developers – Kaan E. Jan 04 '20 at 04:49

4 Answers4

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)
RishiC
  • 768
  • 1
  • 10
  • 34
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
Bi Ao
  • 704
  • 5
  • 11
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]
4ndt3s
  • 3,238
  • 2
  • 20
  • 30
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)
Doc
  • 10,831
  • 3
  • 39
  • 63