0

I want to create a list in python which will be having a variable and its location say a list of 10 values location from 0 to 9 eg at location 0 'Abc', 1 'Cde', 2 'Fgh',.... and so on now I want to use the list values in loop incremented by one

list=['Abc','def','ghi'] # Or this may be an excel file with index and one column having different values

i=0 #has list value 'Abc'
while i<=3:
    a=i
    print('List value at'+i+' equals to',List[i])
i=i+1

Input data frame

enter image description here

select List value at index 0 and assign it to an object and then increment index till all the list values.

Output Required

'List value at 0 equals to Abc'
'List value at 1 equals to def'
'List value at 2 equals to ghi'
'List value at 3 equals to jkl'
'List value at 4 equals to mno'
2
  • Please update your question with the output you require. Commented Apr 17, 2020 at 11:31
  • You have some errors in your code: 1. while i<=3 will result in an exception because your list has only three elements (remember: python lists are zero based) 2. You have list and List 3. Why this a=i ? 4. You have the indentation wrong at i=i+1 Commented Apr 17, 2020 at 12:34

2 Answers 2

1

I really didn't get your question, but hope this helps:

Add element to list

l.append(element)

Get element by position

l[0] # Access element from position 0

Loop through list elements

for i in l:
    print('Item: {}'.format(i))

Loop through list by positions

for i in range(len(l)):
    print('Item: {}'.format(l[i]))

And please, do not name your lists list, it shadows the Python list() function.

Sign up to request clarification or add additional context in comments.

1 Comment

You could maybe add slicing to your answer, since there is a while i<=3: in the question.
0

input: List.xlsx

enter image description here

Process:

k=pd.read_excel('List.xlsx')
m1_list = k.values.tolist()
list = m1_list

i=0
while i < len(list):
    obj= list1[i]
    print(obj)

    i=i+1

output:

'Abc'
'def'
'ghi'
'jkl'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.