Python Forum
Unable to assign selected values from existing list to an empty list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to assign selected values from existing list to an empty list
#1
Hello experts,

Python version : 2.7.10

I am using the below code for assigning the elements : a,b,c,d,e to the list : cities_list.

In order to check my execution, I am using the print statement.

My code :

five_elements = ["a,1","b,2","c,3","d,4","e,5"]
cities_list = []
for i in range(0,len(five_elements)):
    print(five_elements[i][0])
    cities_list.append([i][0])
print(cities_list)
However, I am getting different outputs :
a
b
c
d
e
[0, 1, 2, 3, 4]
I wish to have the output as :
a
b
c
d
e
Could you please help me on this.

Thanks,
Haider
Reply
#2
Hello!
Every element of the list is a string which is iterable. So you just get the first element of that iterable and append it to the new list.

In [1]: five_elements = ["a,1","b,2","c,3","d,4","e,5"]

In [2]: cities_list = []

In [3]: for i in five_elements:
   ...:     cities_list.append(i[0])
   ...:     

In [4]: cities_list
Out[4]: ['a', 'b', 'c', 'd', 'e']
Your way:

In [5]: for i in range(0,len(five_elements)):
   ...:     cities_list.append(five_elements[i][0])
   ...:         

In [6]: cities_list
Out[6]: ['a', 'b', 'c', 'd', 'e']
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
cities_list = 'abcde'
    for n in range(len(cities_list)):
        print(cities_list[n])
Reply
#4
Hmm what are you doing here Larz60+ Huh
range(len(something)) should never be used.
We have discussed this a lot of times.
The same result:
cities_list = 'abcde'
for n in cities_list:
    print(n)
Reply
#5
Ok .. Where did I leave my glasses
Reply
#6
Thanks all...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 223 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  unable to remove all elements from list based on a condition sg_python 3 377 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Code with empty list not executing adeana 9 3,637 Dec-11-2023, 08:27 AM
Last Post: buran
  Copying the order of another list with identical values gohanhango 7 1,067 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,147 Nov-03-2023, 05:35 PM
Last Post: huzzug
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 427 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Comparing List values to get indexes Edward_ 7 1,083 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020