Cyrill
Cyrill

Reputation: 166

Problems regarding conversion of List to Array in Python

What I want to do:

I want to create an array and add each Item from a List to the array. This is what I have so far:

count = 0
arr = []

with open(path,encoding='utf-8-sig') as f:
    data = f.readlines() #Data is the List



for s in data:    
    arr[count] = s
    count+=1

What am I doing wrong? The Error I get is IndexError: list assignment index out of range

Upvotes: 0

Views: 850

Answers (4)

Karma
Karma

Reputation: 661

Seems like you are coming from matlab or R background. when you do arr=[], it creates an empty list, its not an array.

import numpy
count = 0   
with open(path,encoding='utf-8-sig') as f:
    data = f.readlines() #Data is the List

size = len(data)
array = numpy.zeros((size,1))

for s in data:    
    arr[count,0] = s
    count+=1

Upvotes: 0

Haris
Haris

Reputation: 12272

Your arr is an empty array. So, arr[count] = s is giving that error.

Either you initialize your array with empty elements, or use the append method of array. Since you do not know how many elements you will be entering into the array, it is better to use the append method in this case.

for s in data:    
    arr.append(s)
    count+=1

Upvotes: 1

abarnert
abarnert

Reputation: 365945

It's worth taking a step back and asking what you're trying to do here.

f is already an iterable of lines: something you can loop over with for line in f:. But it's "lazy"—once you loop over it once, it's gone. And it's not a sequence—you can loop over it, but you can't randomly access it with indexes or slices like f[20] or f[-10:].

f.readlines() copies that into a list of lines: something you can loop over, and index. While files have the readlines method for this, it isn't really necessary—you can convert any iterable to a list just like this by just calling list(f).

Your loop appears to be an attempt to create another list of the same lines. Which you could do with just list(data). Although it's not clear why you need another list in the first place.


Also, the term "array" betrays some possible confusion.

A Python list is a dynamic array, which can be indexed and modified, but can also be resized by appending, inserting, and deleting elements. So, technically, arr is an array.

But usually when people talk about "arrays" in Python, they mean fixed-size arrays, usually of fixed-size objects, like those provided by the stdlib array module, the third-party numpy library, or special-purpose types like the builtin bytearray.

In general, to convert a list or other iterable into any of these is the same as converting into a list: just call the constructor. For example, if you have a list of numbers between 0-255, you can do bytearray(lst) to get a bytearray of the same numbers. Or, if you have a list of lists of float values, np.array(lst) will give you a 2D numpy array of floats. And so on.


So, why doesn't your code work?

When you write arr = [], you're creating a list of 0 elements.

When you write arr[count] = s, you're trying to set the countth element in the list to s. But there is no countth element. You're writing past the end of the list.

One option is to call arr.append(s) instead. This makes the list 1 element longer than it used to be, and puts s in the new slot.

Another option is to create a list of the right size in the first place, like arr = [None for _ in data]. Then, arr[count] = s can replace the None in the countth slot with s.

But if you really just want a copy of data in another list, you're better off just using arr = list(data), or arr = data[:].

And if you don't have any need for another copy, just do arr = data, or just use data as-is—or even, if it works for your needs, just use f in the first place.

Upvotes: 1

Luke B
Luke B

Reputation: 2121

When you try to access arr at index 0, there is not anything there. What you are trying to do is add to it. You should do arr.append(s)

Upvotes: 4

Related Questions