Python doesn't have arrays like in C++ or Java. Instead you can use a list.
my_list = ['hello','hi','hurra']
for text in my_list:
print(text)
There are multiple ways to iterate the list, e.g.:
for i, text in enumerate(my_list):
print(i, text)
for i in range(0, len(my_list)):
print(my_list[i])
You could use it like "arrays", but there are many build-in functions that you would find very useful. This is a basic and essential data structure in Python. Most of books or tutorials would cover this topic in the first few chapters.
P.S.: The codes are for Python 3.
Answer from ioannu on Stack OverflowPython doesn't have arrays like in C++ or Java. Instead you can use a list.
my_list = ['hello','hi','hurra']
for text in my_list:
print(text)
There are multiple ways to iterate the list, e.g.:
for i, text in enumerate(my_list):
print(i, text)
for i in range(0, len(my_list)):
print(my_list[i])
You could use it like "arrays", but there are many build-in functions that you would find very useful. This is a basic and essential data structure in Python. Most of books or tutorials would cover this topic in the first few chapters.
P.S.: The codes are for Python 3.
You can use dictionaries for this purpose.
mydict={'i1':'hello','i2':'hi','i3':'hurra'}
for i, (key, value) in enumerate(mydict.items()):
print("index: {}, key: {}, value: {}".format(i, key, value))
How to loop through array of strings Python - Stack Overflow
I want to create a loop that will make new arrays for strings of text in a file using indexing (or whatever works). Best way to approach?
Loop through an array of strings
Python loop through array and add to initial array
Can a 'for loop' in Python be nested within another 'for loop'?
How can I iterate over a list using a 'for loop' in Python?
How do I use a 'for loop' to iterate over a dictionary in Python?
Videos
More in-depth:
The way this would theoretically work is that I have a large file of text where each word I want in an array is separated by \n. I want to index this text file so that Python checks “for each time there’s \n, create a new array”. The issue is that I don’t know how to give each array a unique name - or check or \n for that matter.
I have no idea how to do this because I’ve always sucked at using arrays. Any ideas on how this would work?